Last active
August 7, 2017 03:42
-
-
Save nipuntalukdar/5562d5efd6970495e7632399057d5e99 to your computer and use it in GitHub Desktop.
Demo showing synchronizing in using Python multiprocessing Lock
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from multiprocessing import Process, Lock | |
| from time import sleep | |
| def f(name, l, shouldsleep): | |
| i = 1 | |
| while i < 30: | |
| l.acquire() | |
| print 'Myself', name, 'The lock', l | |
| if shouldsleep: | |
| print name, 'will sleep for 10 secs, others have to wait' | |
| sleep(10) | |
| l.release() | |
| i += 1 | |
| if __name__ == '__main__': | |
| l = Lock() | |
| p1 = Process(target=f, args=('first',l, True)) | |
| p1.start() | |
| p2 = Process(target=f, args=('second',l, False)) | |
| p2.start() | |
| p1.join() | |
| p2.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment