多进程+协程 处理IO问题
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)
自动化学习。