Skip to content

Instantly share code, notes, and snippets.

@rochacon
Created January 30, 2013 14:37
Show Gist options
  • Save rochacon/4673687 to your computer and use it in GitHub Desktop.
Save rochacon/4673687 to your computer and use it in GitHub Desktop.
Simple script to test Python's concurrent.futures backport
#!/usr/bin/env python
# pip install futures
from concurrent import futures
import time
import random
def sleep(me, x):
time.sleep(x)
return 'Worker %d sleeped for %s seconds' % (me, x)
with futures.ProcessPoolExecutor(max_workers=30) as executor:
print '--- Launching 10 workers ---'
workers = []
for x in xrange(1, 11):
wait = random.choice(xrange(10))
print 'Worker %d will sleep for %d' % (x, wait)
workers.append(executor.submit(sleep, x, wait))
print '--- Waiting for results ---'
for worker in futures.as_completed(workers):
print worker.result()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment