pymotw.com_2__sources
pymotw.com_2__sources
Snapshot-Content-Location:
https://pymotw.com/2/_sources/multiprocessing/communication.txt
Subject:
Date: Thu, 13 Feb 2025 12:12:05 -0700
MIME-Version: 1.0
Content-Type: multipart/related;
type="text/html";
boundary="----MultipartBoundary--
XvXr0Gl3gMOeOZCaUbsf4lXBJjkFVJuLn5uDdAeu5Z----"
------MultipartBoundary--XvXr0Gl3gMOeOZCaUbsf4lXBJjkFVJuLn5uDdAeu5Z----
Content-Type: text/html
Content-ID: <[email protected]>
Content-Transfer-Encoding: binary
Content-Location: https://pymotw.com/2/_sources/multiprocessing/communication.txt
.. _multiprocessing-queues:
.. include:: multiprocessing_queue.py
:literal:
:start-after: #end_pymotw_header
.. {{{cog
.. cog.out(run_script(cog.inFile, 'multiprocessing_queue.py'))
.. }}}
::
$ python multiprocessing_queue.py
.. {{{end}}}
.. include:: multiprocessing_producer_consumer.py
:literal:
:start-after: #end_pymotw_header
Although the jobs enter the queue in order, since their execution is
parallelized there is no guarantee about the order they will be
completed.
.. {{{cog
.. cog.out(run_script(cog.inFile, '-u multiprocessing_producer_consumer.py'))
.. }}}
::
$ python -u multiprocessing_producer_consumer.py
Creating 16 consumers
Consumer-1: 0 * 0
Consumer-2: 1 * 1
Consumer-3: 2 * 2
Consumer-4: 3 * 3
Consumer-5: 4 * 4
Consumer-6: 5 * 5
Consumer-7: 6 * 6
Consumer-8: 7 * 7
Consumer-9: 8 * 8
Consumer-10: 9 * 9
Consumer-11: Exiting
Consumer-12: Exiting
Consumer-13: Exiting
Consumer-14: Exiting
Consumer-15: Exiting
Consumer-16: Exiting
Consumer-1: Exiting
Consumer-4: Exiting
Consumer-5: Exiting
Consumer-6: Exiting
Consumer-2: Exiting
Consumer-3: Exiting
Consumer-9: Exiting
Consumer-7: Exiting
Consumer-8: Exiting
Consumer-10: Exiting
Result: 0 * 0 = 0
Result: 3 * 3 = 9
Result: 8 * 8 = 64
Result: 5 * 5 = 25
Result: 4 * 4 = 16
Result: 6 * 6 = 36
Result: 7 * 7 = 49
Result: 1 * 1 = 1
Result: 2 * 2 = 4
Result: 9 * 9 = 81
.. {{{end}}}
.. include:: multiprocessing_event.py
:literal:
:start-after: #end_pymotw_header
.. {{{cog
.. cog.out(run_script(cog.inFile, '-u multiprocessing_event.py'))
.. }}}
::
$ python -u multiprocessing_event.py
.. {{{end}}}
.. include:: multiprocessing_lock.py
:literal:
:start-after: #end_pymotw_header
.. {{{cog
.. cog.out(run_script(cog.inFile, 'multiprocessing_lock.py'))
.. }}}
::
$ python multiprocessing_lock.py
.. {{{end}}}
Synchronizing Operations
========================
.. include:: multiprocessing_condition.py
:literal:
:start-after: #end_pymotw_header
.. {{{cog
.. cog.out(run_script(cog.inFile, 'multiprocessing_condition.py'))
.. }}}
::
$ python multiprocessing_condition.py
Starting s1
s1 done and ready for stage 2
Starting stage_2[1]
stage_2[1] running
Starting stage_2[2]
stage_2[2] running
.. {{{end}}}
.. include:: multiprocessing_semaphore.py
:literal:
:start-after: #end_pymotw_header
.. {{{cog
.. cog.out(run_script(cog.inFile, 'multiprocessing_semaphore.py'))
.. }}}
::
$ python multiprocessing_semaphore.py
.. {{{end}}}
.. include:: multiprocessing_manager_dict.py
:literal:
:start-after: #end_pymotw_header
By creating the list through the manager, it is shared and updates are
seen in all processes. Dictionaries are also supported.
.. {{{cog
.. cog.out(run_script(cog.inFile, 'multiprocessing_manager_dict.py'))
.. }}}
::
$ python multiprocessing_manager_dict.py
Results: {0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18}
.. {{{end}}}
Shared Namespaces
=================
.. include:: multiprocessing_namespaces.py
:literal:
:start-after: #end_pymotw_header
.. {{{cog
.. cog.out(run_script(cog.inFile, 'multiprocessing_namespaces.py'))
.. }}}
::
$ python multiprocessing_namespaces.py
.. {{{end}}}
.. include:: multiprocessing_namespaces_mutable.py
:literal:
:start-after: #end_pymotw_header
.. {{{cog
.. cog.out(run_script(cog.inFile, 'multiprocessing_namespaces_mutable.py'))
.. }}}
::
$ python multiprocessing_namespaces_mutable.py
.. {{{end}}}
Process Pools
=============
.. include:: multiprocessing_pool.py
:literal:
:start-after: #end_pymotw_header
.. {{{cog
.. cog.out(run_script(cog.inFile, 'multiprocessing_pool.py'))
.. }}}
::
$ python multiprocessing_pool.py
Input : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Built-in: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
Starting PoolWorker-11
Starting PoolWorker-12
Starting PoolWorker-13
Starting PoolWorker-14
Starting PoolWorker-15
Starting PoolWorker-16
Starting PoolWorker-1
Starting PoolWorker-2
Starting PoolWorker-3
Starting PoolWorker-4
Starting PoolWorker-5
Starting PoolWorker-8
Starting PoolWorker-9
Starting PoolWorker-6
Starting PoolWorker-10
Starting PoolWorker-7
Pool : [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
.. {{{end}}}
.. include:: multiprocessing_pool_maxtasksperchild.py
:literal:
:start-after: #end_pymotw_header
The pool restarts the workers when they have completed their allotted
tasks, even if there is no more work. In this output, eight workers
are created, even though there are only 10 tasks, and each worker can
complete two of them at a time.
.. {{{cog
.. cog.out(run_script(cog.inFile, 'multiprocessing_pool_maxtasksperchild.py'))
.. }}}
::
$ python multiprocessing_pool_maxtasksperchild.py
Input : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Built-in: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
Starting PoolWorker-11
Starting PoolWorker-12
Starting PoolWorker-13
Starting PoolWorker-14
Starting PoolWorker-15
Starting PoolWorker-16
Starting PoolWorker-1
Starting PoolWorker-2
Starting PoolWorker-3
Starting PoolWorker-4
Starting PoolWorker-5
Starting PoolWorker-6
Starting PoolWorker-7
Starting PoolWorker-8
Starting PoolWorker-9
Starting PoolWorker-10
Pool : [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
.. {{{end}}}
</pre></body></html>
------MultipartBoundary--XvXr0Gl3gMOeOZCaUbsf4lXBJjkFVJuLn5uDdAeu5Z------