2626
2727from pytask_parallel .backends import WorkerType
2828from pytask_parallel .backends import registry
29+ from pytask_parallel .backends import set_worker_root
2930from pytask_parallel .typing import CarryOverPath
3031from pytask_parallel .typing import is_coiled_function
3132from pytask_parallel .utils import create_kwargs_for_task
3233from pytask_parallel .utils import get_module
3334from pytask_parallel .utils import parse_future_result
35+ from pytask_parallel .utils import should_pickle_module_by_value
36+ from pytask_parallel .utils import strip_annotation_locals
3437
3538if TYPE_CHECKING :
3639 from concurrent .futures import Future
@@ -57,6 +60,7 @@ def pytask_execute_build(session: Session) -> bool | None: # noqa: C901, PLR091
5760
5861 # The executor can only be created after the collection to give users the
5962 # possibility to inject their own executors.
63+ set_worker_root (session .config ["root" ])
6064 session .config ["_parallel_executor" ] = registry .get_parallel_backend (
6165 session .config ["parallel_backend" ], n_workers = session .config ["n_workers" ]
6266 )
@@ -195,6 +199,7 @@ def pytask_execute_task(session: Session, task: PTask) -> Future[WrapperResult]:
195199 kwargs = create_kwargs_for_task (task , remote = remote )
196200
197201 if is_coiled_function (task ):
202+ strip_annotation_locals (task )
198203 # Prevent circular import for coiled backend.
199204 from pytask_parallel .wrappers import ( # noqa: PLC0415
200205 rewrap_task_with_coiled_function ,
@@ -208,7 +213,8 @@ def pytask_execute_task(session: Session, task: PTask) -> Future[WrapperResult]:
208213 # cloudpickle will pickle it with the function. See cloudpickle#417, pytask#373
209214 # and pytask#374.
210215 task_module = get_module (task .function , getattr (task , "path" , None ))
211- cloudpickle .register_pickle_by_value (task_module )
216+ if should_pickle_module_by_value (task_module ):
217+ cloudpickle .register_pickle_by_value (task_module )
212218
213219 return cast ("Any" , wrapper_func ).submit (
214220 task = task ,
@@ -221,6 +227,7 @@ def pytask_execute_task(session: Session, task: PTask) -> Future[WrapperResult]:
221227 )
222228
223229 if worker_type == WorkerType .PROCESSES :
230+ strip_annotation_locals (task )
224231 # Prevent circular import for loky backend.
225232 from pytask_parallel .wrappers import wrap_task_in_process # noqa: PLC0415
226233
@@ -230,7 +237,8 @@ def pytask_execute_task(session: Session, task: PTask) -> Future[WrapperResult]:
230237 # cloudpickle will pickle it with the function. See cloudpickle#417, pytask#373
231238 # and pytask#374.
232239 task_module = get_module (task .function , getattr (task , "path" , None ))
233- cloudpickle .register_pickle_by_value (task_module )
240+ if should_pickle_module_by_value (task_module ):
241+ cloudpickle .register_pickle_by_value (task_module )
234242
235243 return session .config ["_parallel_executor" ].submit (
236244 wrap_task_in_process ,
0 commit comments