Skip to content

Instantly share code, notes, and snippets.

@lbolla
Created April 25, 2016 09:31
Show Gist options
  • Save lbolla/92bad9f4320940ac2f762424ac840a12 to your computer and use it in GitHub Desktop.
Save lbolla/92bad9f4320940ac2f762424ac840a12 to your computer and use it in GitHub Desktop.
import time
import multiprocessing.queues
from Queue import Empty
import futures
q = multiprocessing.queues.Queue()
N = 10000
M = 4
timeout = 1
pool = futures.ThreadPoolExecutor(M)
def feed(_i):
for i in xrange(N):
q.put(i)
def drain(_i):
for _ in xrange(N):
q.get()
def drain_nowait(_i):
for _ in xrange(N):
rs = None
# Keep polling until we get something
while rs is None:
try:
rs = q.get(False, timeout)
except Empty:
pass
t0 = time.time()
for r in pool.map(feed, xrange(M)):
pass
t1 = time.time()
for r in pool.map(drain, xrange(M)):
pass
t2 = time.time()
# Feed it again
for r in pool.map(feed, xrange(M)):
pass
t3 = time.time()
for r in pool.map(drain_nowait, xrange(M)):
pass
t4 = time.time()
print 'feed={} drain={} drain_nowait={}'.format(
t1 - t0, t2 - t1, t4 - t3)
@shiplu
Copy link

shiplu commented Sep 28, 2018

In version 2.7 How do you get futures?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment