def gcd(pair):
a, b = pair
low = min(a, b)
for i in range(low, 0, -1):
if a % i == 0 and b % i == 0:
return i
numbers = [
(1963309, 2265973), (1879675, 2493670), (2030677, 3814172),
(1551645, 2229620), (1988912, 4736670), (2198964, 7876293)
import time
start = time.time()
results = list(map(gcd, numbers))
end = time.time()
print 'Took %.3f seconds.' % (end - start)
Took 2.507 seconds.
import time
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor, Executor
start = time.time()
pool = ProcessPoolExecutor(max_workers=2)
results = list(pool.map(gcd, numbers))
end = time.time()
print 'Took %.3f seconds.' % (end - start)
Took 1.861 seconds.
fn(*args **kwargs)Future
with ThreadPoolExecutor(max_workers=1) as executor:
future = executor.submit(pow, 323, 1235)
print(future.result())
map(func,* iterables,timeout = None,chunksize = 1 )
与以下类似:map(func, *iterables)
在iterables收集立即而不是懒洋洋地;
func以异步方式执行,并且可以同时对func进行多次调用 。
返回的迭代器引发一个concurrent.futures.TimeoutError if
next
()被调用,并且在从原始调用到超时秒后结果不可用Executor.map()。 timeout可以是int或float。如果未指定 超时None,则等待时间没有限制。