Created
April 25, 2016 09:31
-
-
Save lbolla/92bad9f4320940ac2f762424ac840a12 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 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In version 2.7 How do you get futures?