Skip to content

Instantly share code, notes, and snippets.

@mdeous
Created March 11, 2011 22:09
Show Gist options
  • Save mdeous/866667 to your computer and use it in GitHub Desktop.
Save mdeous/866667 to your computer and use it in GitHub Desktop.
threads synchronization
from async import async
from time import sleep
class ThreadSyncer(object):
def __init__(self, threads_amount):
self.amount = threads_amount
self.finished_count = 0
def thread_ended(self):
self.finished_count += 1
def wait_for_threads(self, callback):
while self.finished_count < self.amount:
continue
callback()
syncer = ThreadSyncer(2)
@async(callback=syncer.thread_ended, errback=syncer.thread_ended)
def my_first_function():
sleep(5)
@async(callback=syncer.thread_ended, errback=syncer.thread_ended)
def my_other_function():
sleep(3)
def last_function():
print "Finished"
my_first_function()
my_other_function()
syncer.wait_for_threads(last_function)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment