Created
January 30, 2013 14:37
-
-
Save rochacon/4673687 to your computer and use it in GitHub Desktop.
Simple script to test Python's concurrent.futures backport
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
#!/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