[issue34051] Update multiprocessing example

Windson Yang report at bugs.python.org
Thu Jul 5 04:45:49 EDT 2018


New submission from Windson Yang <wiwindson at gmail.com>:

The docs at https://docs.python.org/3.8/library/multiprocessing.html#synchronization-between-processes give an example:

from multiprocessing import Process, Lock

def f(l, i):
    l.acquire()
    try:
        print('hello world', i)
    finally:
        l.release()

if __name__ == '__main__':
    lock = Lock()

    for num in range(10):
        Process(target=f, args=(lock, num)).start()

and point out "For instance one can use a lock to ensure that only one process prints to standard output at a time...". I'm not sure this is a good enough example for the reader. The reader can't tell the difference between the function with l.acquire() or not, The output just shows in the terminal at the same time. So I think a better idea just add time.sleep(0.1) before print('hello world', i) like this:

    l.acquire()
    try:
        # do something here
        # time.sleep(0.1)
        print('hello world', i)

I can provide a pr if you guys like this idea.

----------
assignee: docs at python
components: Documentation
messages: 321088
nosy: Windson Yang, docs at python
priority: normal
severity: normal
status: open
title: Update multiprocessing example
versions: Python 3.8

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue34051>
_______________________________________


More information about the Python-bugs-list mailing list