@@ -47,15 +47,15 @@ $ pytask --n-workers 2
4747$ pytask -n auto
4848```
4949
50- Using processes to parallelize the execution of tasks is useful for CPU bound tasks such
50+ Using processes to parallelize the execution of tasks is useful for CPU- bound tasks such
5151as numerical computations. ([ Here] ( https://stackoverflow.com/a/868577/7523785 ) is an
52- explanation on what CPU or IO bound means.)
52+ explanation of what CPU- or IO- bound means.)
5353
54- For IO bound tasks, tasks where the limiting factor are network responses, access to
54+ For IO- bound tasks, tasks where the limiting factor is network latency and access to
5555files, you can parallelize via threads.
5656
5757``` console
58- $ pytask --parallel-backend threads
58+ pytask --parallel-backend threads
5959```
6060
6161You can also set the options in a ` pyproject.toml ` .
@@ -68,6 +68,65 @@ n_workers = 1
6868parallel_backend = " processes" # or loky or threads
6969```
7070
71+ ## Custom Executor
72+
73+ > [ !NOTE]
74+ >
75+ > The interface for custom executors is rudimentary right now and there is not a lot of
76+ > support by public functions. Please, give some feedback if you are trying or managed
77+ > to use a custom backend.
78+ >
79+ > Also, please contribute your custom executors if you consider them useful to others.
80+
81+ pytask-parallel allows you to use your parallel backend as long as it follows the
82+ interface defined by
83+ [ ` concurrent.futures.Executor ` ] ( https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.Executor ) .
84+
85+ In some cases, adding a new backend can be as easy as registering a builder function
86+ that receives some arguments (currently only ` n_workers ` ) and returns the instantiated
87+ executor.
88+
89+ ``` python
90+ from concurrent.futures import Executor
91+ from my_project.executor import CustomExecutor
92+
93+ from pytask_parallel import ParallelBackend, registry
94+
95+
96+ def build_custom_executor (n_workers : int ) -> Executor:
97+ return CustomExecutor(max_workers = n_workers)
98+
99+
100+ registry.register_parallel_backend(ParallelBackend.CUSTOM , build_custom_executor)
101+ ```
102+
103+ Now, build the project requesting your custom backend.
104+
105+ ``` console
106+ pytask --parallel-backend custom
107+ ```
108+
109+ Realistically, it is not the only necessary adjustment for a nice user experience. There
110+ are two other important things. pytask-parallel does not implement them by default since
111+ it seems more tightly coupled to your backend.
112+
113+ 1 . A wrapper for the executed function that captures warnings, catches exceptions and
114+ saves products of the task (within the child process!).
115+
116+ As an example, see
117+ [ ` def _execute_task() ` ] ( https://github.com/pytask-dev/pytask-parallel/blob/c441dbb75fa6ab3ab17d8ad5061840c802dc1c41/src/pytask_parallel/processes.py#L91-L155 )
118+ that does all that for the processes and loky backend.
119+
120+ 1 . To apply the wrapper, you need to write a custom hook implementation for
121+ ` def pytask_execute_task() ` . See
122+ [ ` def pytask_execute_task() ` ] ( https://github.com/pytask-dev/pytask-parallel/blob/c441dbb75fa6ab3ab17d8ad5061840c802dc1c41/src/pytask_parallel/processes.py#L41-L65 )
123+ for an example. Use the
124+ [ ` hook_module ` ] ( https://pytask-dev.readthedocs.io/en/stable/how_to_guides/extending_pytask.html#using-hook-module-and-hook-module )
125+ configuration value to register your implementation.
126+
127+ Another example of an implementation can be found as a
128+ [ test] ( https://github.com/pytask-dev/pytask-parallel/blob/c441dbb75fa6ab3ab17d8ad5061840c802dc1c41/tests/test_backends.py#L35-L78 ) .
129+
71130## Some implementation details
72131
73132### Parallelization and Debugging
@@ -92,12 +151,12 @@ Consult the [release notes](CHANGES.md) to find out about what is new.
92151- ` pytask-parallel ` does not call the ` pytask_execute_task_protocol ` hook
93152 specification/entry-point because ` pytask_execute_task_setup ` and
94153 ` pytask_execute_task ` need to be separated from ` pytask_execute_task_teardown ` . Thus,
95- plugins which change this hook specification may not interact well with the
154+ plugins that change this hook specification may not interact well with the
96155 parallelization.
97156
98- - There are two PRs for CPython which try to re-enable setting custom reducers which
99- should have been working, but does not. Here are the references.
157+ - Two PRs for CPython try to re-enable setting custom reducers which should have been
158+ working but does not. Here are the references.
100159
101- > - < https://bugs.python.org/issue28053 >
102- > - < https://github.com/python/cpython/pull/9959 >
103- > - < https://github.com/python/cpython/pull/15058 >
160+ - https://bugs.python.org/issue28053
161+ - https://github.com/python/cpython/pull/9959
162+ - https://github.com/python/cpython/pull/15058
0 commit comments