Skip to content

Instantly share code, notes, and snippets.

@lacek
Created July 29, 2015 06:42
Show Gist options
  • Save lacek/41ab2dea749e9d614864 to your computer and use it in GitHub Desktop.
Save lacek/41ab2dea749e9d614864 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import gevent
from gevent.threadpool import ThreadPool
from random import randint
def worker(id):
print('worker %d working...' % (id))
time = randint(1, 5)
gevent.sleep(time)
return (id, time)
pool = ThreadPool(2)
results = pool.imap(worker, xrange(0, 3))
for id, time in results:
print('sequential worker %d done for %s seconds' % (id, time))
results = pool.imap(worker, xrange(3,6))
pool.join()
for id, time in results:
print('batch worker %d done for %s seconds' % (id, time))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment