[issue34051] Update multiprocessing example
New submission from Windson Yang <wiwindson@gmail.com>: The docs at https://docs.python.org/3.8/library/multiprocessing.html#synchronization-bet... 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 <report@bugs.python.org> <https://bugs.python.org/issue34051> _______________________________________
Change by Raymond Hettinger <raymond.hettinger@gmail.com>: ---------- assignee: docs@python -> davin nosy: +davin _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue34051> _______________________________________
Change by Windson Yang <wiwindson@gmail.com>: ---------- nosy: +zach.ware _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue34051> _______________________________________
Antoine Pitrou <pitrou@free.fr> added the comment: The example feels a bit artificial indeed, but I don't think adding a sleep() call would make it realistic. Why would you protect sleep() with a lock? ---------- nosy: +pitrou _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue34051> _______________________________________
Windson Yang <wiwindson@gmail.com> added the comment: Hello, @Antoine Pitrou. Maybe there is another way to let the reader know "only one process prints to standard output at a time" instead of sleep() function? ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue34051> _______________________________________
Antoine Pitrou <pitrou@free.fr> added the comment: To be frank, I don't think that matters much. The user should understand what a lock is already, if they want to make use of multiprocessing fruitfully. The example showcases how to create a lock and how to pass it to child processes (by giving it as a function parameter). Printing to standard output is not the important thing here. However if you want to improve this example, you could replace the acquire/release pair with a "with" statement. ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue34051> _______________________________________
Windson Yang <wiwindson@gmail.com> added the comment: Thank you, I think to use acquire() and release() may be better than with statement in this example. I will close this issue. ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue34051> _______________________________________
Change by Windson Yang <wiwindson@gmail.com>: ---------- stage: -> resolved status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue34051> _______________________________________
participants (3)
-
Antoine Pitrou
-
Raymond Hettinger
-
Windson Yang