码迷,mamicode.com
首页 > 编程语言 > 详细

python3 进程线程协程 并发查找列表

时间:2020-12-04 10:52:40      阅读:6      评论:0      收藏:0      [点我收藏+]

标签:gevent   gre   pat   star   tar   find   查找   spl   import   

code

import 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 % 

 

 

 

 

 

 

 

 

python3 进程线程协程 并发查找列表

标签:gevent   gre   pat   star   tar   find   查找   spl   import   

原文地址:https://www.cnblogs.com/sea-stream/p/14057824.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!