Created
March 11, 2011 22:09
-
-
Save mdeous/866667 to your computer and use it in GitHub Desktop.
threads synchronization
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 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