Skip to content

Instantly share code, notes, and snippets.

@nipuntalukdar
Last active August 7, 2017 03:42
Show Gist options
  • Select an option

  • Save nipuntalukdar/5562d5efd6970495e7632399057d5e99 to your computer and use it in GitHub Desktop.

Select an option

Save nipuntalukdar/5562d5efd6970495e7632399057d5e99 to your computer and use it in GitHub Desktop.
Demo showing synchronizing in using Python multiprocessing Lock
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