用多进程ProcessPoolExecutor规避GIL对Python多核心利用率的限制

linker   ·   发表于 21天前   ·   Python
# -*- coding:utf-8 -*-
# 求最大公约数
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 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.
0 Reply   |  Until 21天前 | 17 View
LoginCan Publish Content
程序耗时 0.0219秒 内存:363KB