Skip to content

Commit 6b05953

Browse files
committed
Fix process pool executor docs
1 parent f5618b0 commit 6b05953

File tree

2 files changed

+268
-46
lines changed

2 files changed

+268
-46
lines changed

aiomisc/process_pool.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,59 @@ class ProcessPoolStatistic(Statistic):
1818

1919

2020
class ProcessPoolExecutor(ProcessPoolExecutorBase, EventLoopMixin):
21+
"""
22+
Process pool executor with statistic
23+
24+
Usage:
25+
26+
.. code-block:: python
27+
28+
from time import sleep
29+
from aiomisc import ProcessPoolExecutor
30+
31+
# NOTE: blocking function must be defined at the top level
32+
# of the module to be able to be pickled and sent to the
33+
# child processes.
34+
def blocking_fn():
35+
sleep(1)
36+
return 42
37+
38+
async def main():
39+
executor = ProcessPoolExecutor()
40+
the_answer = await executor.submit(blocking_fn)
41+
print("The answer is:", the_answer)
42+
43+
asyncio.run(main())
44+
"""
45+
2146
DEFAULT_MAX_WORKERS = max((cpu_count(), 4))
2247

2348
def __init__(self, max_workers: int = DEFAULT_MAX_WORKERS, **kwargs: Any):
49+
"""
50+
Initializes a new ProcessPoolExecutor instance.
51+
52+
* ``max_workers``:
53+
The maximum number of processes that can be used to
54+
execute the given calls. If None or not given then
55+
as many worker processes will be created as the
56+
machine has processors.
57+
* ``mp_context``:
58+
A multiprocessing context to launch the workers. This
59+
object should provide SimpleQueue, Queue and Process.
60+
Useful to allow specific multiprocessing start methods.
61+
* ``initializer``:
62+
A callable used to initialize worker processes.
63+
* ``initargs``:
64+
A tuple of arguments to pass to the initializer.
65+
* ``max_tasks_per_child``:
66+
The maximum number of tasks a worker process
67+
can complete before it will exit and be replaced
68+
with a fresh worker process. The default of None
69+
means worker process will live as long as the
70+
executor. Requires a non-'fork' mp_context start
71+
method. When given, we default to using 'spawn'
72+
if no mp_context is supplied.
73+
"""
2474
super().__init__(max_workers=max_workers, **kwargs)
2575
self._statistic = ProcessPoolStatistic()
2676
self._statistic.processes = max_workers
@@ -31,6 +81,9 @@ def _statistic_callback(
3181
start_time: float,
3282
loop: asyncio.AbstractEventLoop,
3383
) -> None:
84+
"""
85+
Callback for statistic
86+
"""
3487
if future.exception():
3588
self._statistic.error += 1
3689
else:
@@ -52,4 +105,7 @@ def submit(self, *args: Any, **kwargs: Any) -> Future:
52105
return future
53106

54107
def __del__(self) -> None:
108+
"""
109+
Cleanup resources
110+
"""
55111
self.shutdown()

0 commit comments

Comments
 (0)