python - Lock in multiprocessing package not working -
the following code simple, testing purposes, not getting output desired:
from multiprocessing import process,lock def printing(l,i): l.acquire() print l.release() if __name__ == '__main__': lock = lock() in range(10): process(target=printing,args=(lock,i)).start()
the output is:
0 1 2 3 5 6 4 7 8 9
locks supposed suspend other processes executing. why isn't happening here?
what output did expect? output looks fine me: permutation of range(10)
. order in processes happen execute may vary run run. that's expected.
i suspect misunderstand lock does. when acquire lock, every other process trying acquire same lock blocks until lock released. that's all. in test run, process 0 happened acquire lock first. other processes trying acquire lock blocked until process 0 releases lock. process 0 prints 0
, releases lock. happened process 1 acquired lock. etc.
comment out l.release()
, , you'll see program never finishes: first process happens acquire lock prints integer, , other processes forever blocked waiting acquire lock.
Comments
Post a Comment