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. |