python3 进程线程协程 并发查找列表
2021-03-13 10:43
标签:gevent gre pat star tar find 查找 spl import code 输出 python3 进程线程协程 并发查找列表 标签:gevent gre pat star tar find 查找 spl import 原文地址:https://www.cnblogs.com/sea-stream/p/14057824.htmlimport multiprocessing
from multiprocessing import Pool,Queue
import time
import threading
import gevent
from gevent import monkey
monkey.patch_all() #gevent三行放在其他所有import语句之前可以避免出现警告或者报错信息,导致程序不能正常运行
def process_data(data,que,index):
for i in data:
if(i==8900):
que.put(i)
pass
def get_in_gevent(data,que,index):
searchl=data
gevent_pool=[]
pro_num=10
#每个进程处理任务数量
per_num=int(len(searchl)/pro_num)
#余数
lef=len(searchl)%pro_num
for i in range(pro_num):
if(i==(pro_num-1) and (not lef==0)):
tmp = gevent.spawn(process_data,searchl[per_num*i:per_num*(i+1)+lef],que,i)
else:
tmp=gevent.spawn(process_data,searchl[per_num*i:per_num*(i+1)],que,i)
tmp.name = "gevent{}".format(i)
gevent_pool.append(tmp)
tmp.start()
for j in gevent_pool:
j.join()
def get_in_thread(data,que,index):
searchl=data
thread_pool=[]
pro_num=10
#每个进程处理任务数量
per_num=int(len(searchl)/pro_num)
#余数
lef=len(searchl)%pro_num
for i in range(pro_num):
if(i==(pro_num-1) and (not lef==0)):
tmp=threading.Thread(target=get_in_gevent,args=(searchl[per_num*i:per_num*(i+1)+lef],que,i))
else:
tmp=threading.Thread(target=get_in_gevent,args=(searchl[per_num*i:per_num*(i+1)],que,i))
tmp.name = "thread{}".format(i)
tmp.daemon = True
thread_pool.append(tmp)
tmp.start()
for j in thread_pool:
j.join()
if __name__ == ‘__main__‘:
que=Queue()
searchl=[ i for i in range(1000000) ]
pro_pool=[]
pro_num=12
#每个进程处理任务数量
per_num=int(len(searchl)/pro_num)
#余数
lef=len(searchl)%pro_num
for i in range(pro_num):
if(i==(pro_num-1) and (not lef==0)):
tmp = multiprocessing.Process(target=get_in_thread,args=(searchl[per_num*i:per_num*(i+1)+lef],que,i))
else:
tmp = multiprocessing.Process(target=get_in_thread,args=(searchl[per_num*i:per_num*(i+1)],que,i))
tmp.name = "progress{}".format(i)
tmp.daemon = True
pro_pool.append(tmp)
tmp.start()
while(True):
v=que.get()
if(v):
print("find",v)
break
print("main")
macname@MacdeMBP Desktop % python3 test.py
find 8900
main
macname@MacdeMBP Desktop %
上一篇:什么是 Spring Boot?
下一篇:Cooperative Vehicular Content Distribution in Edge Computing Assisted 5G-VANET
文章标题:python3 进程线程协程 并发查找列表
文章链接:http://soscw.com/index.php/essay/64094.html