Created
January 7, 2019 15:09
-
-
Save sagivmalihi/ad843e9f917e5f16fcf88860f92355e4 to your computer and use it in GitHub Desktop.
This file contains 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
import gevent.monkey; gevent.monkey.patch_all(); | |
import gevent.queue | |
import functools | |
import time | |
import random | |
Q = gevent.queue.Queue() | |
QUIT = "QUIT" | |
def launcher(): | |
coroutines = [] | |
for task in Q: | |
if task == QUIT: | |
break | |
print "going to launch %s" % task | |
coroutine = gevent.spawn(task) | |
coroutines.append(coroutine) | |
print "Waiting for all coroutines..." | |
gevent.joinall(coroutines) | |
def task(arg): | |
print "task %s starting" % arg | |
time.sleep(random.random() * 10) | |
print "task %s done" % arg | |
def main(): | |
launcher_routine = gevent.spawn(launcher) | |
counter = 1 | |
try: | |
while True: | |
Q.put(functools.partial(task, counter)) | |
counter += 1 | |
time.sleep(random.random() * 10) | |
except KeyboardInterrupt: | |
Q.put(QUIT) | |
launcher_routine.join() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment