New submission from Windson Yang <wiwind...@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@python
components: Documentation
messages: 321088
nosy: Windson Yang, docs@python
priority: normal
severity: normal
status: open
title: Update multiprocessing example
versions: Python 3.8

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue34051>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to