1. 程式人生 > >多進程+協程 處理IO問題

多進程+協程 處理IO問題

運算 class tip async pen int multi () lse

from multiprocessing import Pool
import gevent,os
import time


def recursion(n):
    if n == 1 or n ==2:
        return 1
    else:
        return recursion(n-1)+recursion(n-2)


def f1(n):
    for i in range(1,n):
        print(%s遞歸運算%i,recursion(i))
        gevent.sleep(0.01)

def f2(n): s = [] for i in range(n): s.append(i*i) print(%s列表計算%i,s[i]) gevent.sleep(0.1) def gevent_xc(num_1): g = [gevent.spawn(f1,num_1)] for item in g: item.join() def gevent_2(num_1): g = [gevent.spawn(f2, num_1)]
for item in g: item.join() if __name__==__main__: start = time.clock() # 測試協程 # g1 = gevent.spawn(f1,34) # g2 = gevent.spawn(f2,35) # # g1.join() # g2.join() # 測試進程 pool = Pool(3) pool.apply_async(gevent_xc,(34,)) pool.apply_async(gevent_2,(
35,)) pool.close() pool.join() print(time.clock()-start)

多進程+協程 處理IO問題