[Python-checkins] Update logging cookbook to show multiple worker processes using the concurrent.futures module. (GH-14905) (GH-14907)

Vinay Sajip webhook-mailer at python.org
Mon Jul 22 08:25:28 EDT 2019


https://github.com/python/cpython/commit/5d3d0f382f6663fcb8b426a62ef3a4cbd9a938d6
commit: 5d3d0f382f6663fcb8b426a62ef3a4cbd9a938d6
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: Vinay Sajip <vinay_sajip at yahoo.co.uk>
date: 2019-07-22T13:25:22+01:00
summary:

Update logging cookbook to show multiple worker processes using the concurrent.futures module. (GH-14905) (GH-14907)

(cherry picked from commit d309352c6fd93a51f2b3011ca8c2125d3a5d394b)

files:
M Doc/howto/logging-cookbook.rst

diff --git a/Doc/howto/logging-cookbook.rst b/Doc/howto/logging-cookbook.rst
index 105ae5ed25a0..a9222ab6ce38 100644
--- a/Doc/howto/logging-cookbook.rst
+++ b/Doc/howto/logging-cookbook.rst
@@ -949,6 +949,41 @@ This variant shows how you can e.g. apply configuration for particular loggers
 machinery in the main process (even though the logging events are generated in
 the worker processes) to direct the messages to the appropriate destinations.
 
+Using concurrent.futures.ProcessPoolExecutor
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+If you want to use :class:`concurrent.futures.ProcessPoolExecutor` to start
+your worker processes, you need to create the queue slightly differently.
+Instead of
+
+.. code-block:: python
+
+   queue = multiprocessing.Queue(-1)
+
+you should use
+
+.. code-block:: python
+
+   queue = multiprocessing.Manager().Queue(-1)  # also works with the examples above
+
+and you can then replace the worker creation from this::
+
+    workers = []
+    for i in range(10):
+        worker = multiprocessing.Process(target=worker_process,
+                                         args=(queue, worker_configurer))
+        workers.append(worker)
+        worker.start()
+    for w in workers:
+        w.join()
+
+to this (remembering to first import :mod:`concurrent.futures`)::
+
+    with concurrent.futures.ProcessPoolExecutor(max_workers=10) as executor:
+        for i in range(10):
+            executor.submit(worker_process, queue, worker_configurer)
+
+
 Using file rotation
 -------------------
 



More information about the Python-checkins mailing list