From aabda6eb4ea22bff6a61c7b478b4573882018ce1 Mon Sep 17 00:00:00 2001 From: William Patton Date: Sat, 11 May 2024 12:50:40 +0200 Subject: [PATCH] use dill to store the spawn function as bytes when creating a worker, we pass in a spawn function. Dill is an improved version of pickle so dill can serialize many more functions than pickle can. Thus we use dill to serialize the spawn function to bytes, which can then be serialized/deserialized by pickle when spawning subprocesses. Then when the worker needs to run the spawn function, we can use dill to deserialize the bytes into the desired function. --- daisy/worker.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/daisy/worker.py b/daisy/worker.py index 12e05e7f..30789232 100644 --- a/daisy/worker.py +++ b/daisy/worker.py @@ -4,6 +4,7 @@ import multiprocessing import os import queue +import dill logger = logging.getLogger(__name__) @@ -50,6 +51,14 @@ def __init__(self, spawn_function, context=None, error_queue=None): self.start() + @property + def spawn_function(self): + return dill.loads(self._spawn_function) + + @spawn_function.setter + def spawn_function(self, value): + self._spawn_function = dill.dumps(value) + def start(self): """Start this worker. Note that workers are automatically started when created. Use this function to re-start a stopped worker."""