Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d309352

Browse files
authored
Update logging cookbook to show multiple worker processes using the concurrent.futures module. (#14905)
1 parent 7397cda commit d309352

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

Doc/howto/logging-cookbook.rst

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,41 @@ This variant shows how you can e.g. apply configuration for particular loggers
948948
machinery in the main process (even though the logging events are generated in
949949
the worker processes) to direct the messages to the appropriate destinations.
950950

951+
Using concurrent.futures.ProcessPoolExecutor
952+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
953+
954+
If you want to use :class:`concurrent.futures.ProcessPoolExecutor` to start
955+
your worker processes, you need to create the queue slightly differently.
956+
Instead of
957+
958+
.. code-block:: python
959+
960+
queue = multiprocessing.Queue(-1)
961+
962+
you should use
963+
964+
.. code-block:: python
965+
966+
queue = multiprocessing.Manager().Queue(-1) # also works with the examples above
967+
968+
and you can then replace the worker creation from this::
969+
970+
workers = []
971+
for i in range(10):
972+
worker = multiprocessing.Process(target=worker_process,
973+
args=(queue, worker_configurer))
974+
workers.append(worker)
975+
worker.start()
976+
for w in workers:
977+
w.join()
978+
979+
to this (remembering to first import :mod:`concurrent.futures`)::
980+
981+
with concurrent.futures.ProcessPoolExecutor(max_workers=10) as executor:
982+
for i in range(10):
983+
executor.submit(worker_process, queue, worker_configurer)
984+
985+
951986
Using file rotation
952987
-------------------
953988

0 commit comments

Comments
 (0)