天天看點

程序、線程、協程開啟方式

__author__ = 'admin'
from gevent import monkey
monkey.patch_all(thread=False)
import gevent,time,os
from threading import Thread,currentThread
from multiprocessing import Process,Pool,current_process
from concurrent.futures import ProcessPoolExecutor,ThreadPoolExecutor

def thread_fun(item):
    print('\033[33m <name:%s> <value:%s> <pid:%s> start...'%(currentThread().name,item,os.getpid()))
    time.sleep(1)
    print('\033[33m <name:%s> <value:%s> <pid:%s> end...'%(currentThread().name,item,os.getpid()))

def process_fun(item):
    print('\033[35m <name:%s> <value:%s> <pid:%s> start...'%(current_process().name,item,os.getpid()))
    time.sleep(1)
    print('\033[35m <name:%s> <value:%s> <pid:%s> start...'%(current_process().name,item,os.getpid()))

def gevent_fun(item):
    print('\033[36m <name:%s> <value:%s> <pid:%s> start...'%(currentThread().name,item,os.getpid()))
    time.sleep(1)
    print('\033[36m <name:%s> <value:%s> <pid:%s> end...'%(currentThread().name,item,os.getpid()))
    return item**2

def thread_pool_fun(item):
    print('\033[32m <name:%s> <value:%s> <pid:%s> start...'%(currentThread().name,item,os.getpid()))
    time.sleep(1)
    print('\033[32m <name:%s> <value:%s> <pid:%s> end...'%(currentThread().name,item,os.getpid()))
    return item**2

def thread_pool_fun_callback(item):
    print('\033[31m <name:%s> <value:%s> <pid:%s> start...'%(currentThread().name,item,os.getpid()))
    time.sleep(1)
    print('\033[31m <name:%s> <value:%s> <pid:%s> end...'%(currentThread().name,item,os.getpid()))

def process_pool_fun(item):
    print('\033[30m <name:%s> <value:%s> <pid:%s> start...'%(current_process().name,item,os.getpid()))
    time.sleep(1)
    print('\033[30m <name:%s> <value:%s> <pid:%s> end...'%(current_process().name,item,os.getpid()))
    return item**2

def process_pool_fun_callback(item):
    print('\033[29m <name:%s> <value:%s> <pid:%s> start...'%(current_process().name,item,os.getpid()))
    time.sleep(1)
    print('\033[29m <name:%s> <value:%s> <pid:%s> end...'%(current_process().name,item,os.getpid()))

def pool_fun(item):
    print('\033[28m <name:%s> <value:%s> <pid:%s> start...'%(current_process().name,item,os.getpid()))
    time.sleep(1)
    print('\033[28m <name:%s> <value:%s> <pid:%s> end...'%(current_process().name,item,os.getpid()))
    return item**2

def pool_fun_callback(item):
    print('\033[33m <name:%s> <value:%s> <pid:%s> start...'%(current_process().name,item,os.getpid()))
    time.sleep(1)
    print('\033[33m <name:%s> <value:%s> <pid:%s> end...'%(current_process().name,item,os.getpid()))
    return item**2

if __name__ == '__main__':
    # 開啟單純程序、線程、協成的方法
    def thread(value):
        threads = []
        for i in range(value):
            t = Thread(target=thread_fun,args=(i,))
            threads.append(t)
            t.start()
        for obj in threads:
            obj.join()
        print('--------->thread is end')
    def process(value):
        processs = []
        for i in range(value):
            p = Process(target=process_fun,args=(i,))
            processs.append(p)
            p.start()
        for obj in processs:
            obj.join()
        print('------>process is end')
    def gev(value):
        Gevents = []
        for i in range(value):
            g = gevent.spawn(gevent_fun,i)
            Gevents.append(g)
        gevent.joinall(Gevents)
        res = [obj.value for obj in Gevents]
        print(res)
        print('------>gevent is end')
    def thread_pool(value):
        thp = ThreadPoolExecutor(4)
        targets = []
        for i in range(value):
            target = thp.submit(thread_pool_fun,i)
            # target = thp.submit(thread_pool_fun,i).add_done_callback(thread_pool_fun_callback)
            #這種方式使用回調函數,就不能再擷取函數(第一個、第二個)的傳回值,如果使用result()會報錯,報錯原因是沒有result屬性
            targets.append(target)
        thp.shutdown()
        res = [obj.result() for obj in targets]
        # obj = thp.map(thread_pool_fun,range(5))
        # res = list(obj)
        print(res)
        print('------>ThreadPoolExecutor is end')
    def process_pool(value):
        pop = ProcessPoolExecutor(4)
        targets = []
        for i in range(value):
            target = pop.submit(process_pool_fun,i)
            # target = pop.submit(process_pool_fun,i).add_done_callback(process_pool_fun_callback)
            # 這種方式使用回調函數,就不能再擷取函數(第一個、第二個)的傳回值,如果使用result()會報錯,報錯原因是沒有result屬性
            targets.append(target)
        pop.shutdown()
        res = [obj.result() for obj in targets]
        # obj = pop.map(process_pool_fun,range(value))
        # res = list(obj)
        print(res)
        print('------>ProcessPoolExecutor is end')
    def pool(value):
        targets = []
        p = Pool(4)
        for i in range(value):
            target = p.apply_async(func=pool_fun,args=(i,))
            # target = p.apply_async(func=pool_fun,args=(i,),callback=pool_fun_callback)
            # 注意一點的是,在回調函數下使用get()方法取得的結果仍然是第一個函數處理後的結果
            targets.append(target)
        p.close()
        p.join()
        res = [obj.get() for obj in targets]
        # obj = p.map(pool_fun,range(5))
        # obj = p.map_async(pool_fun,range(5))
        # map()函數的使用
        # res = list(obj)#第一種map的擷取結果方式,是個清單
        # res = obj.get()#第二種map的擷取結果方式
        print(res)
        print('------>processpool is end')
    #開啟組合多程序、線程、協成的方法
    def run(process_num,thread_num,gevent_num):
        t_pool = ThreadPoolExecutor(4)
        p_pool = ProcessPoolExecutor(4)
        processs = []
        threads = []
        gevents = []
        for i in range(process_num):
            obj = p_pool.submit(process_pool_fun,i)
            processs.append(obj)
        for i in range(thread_num):
            obj = t_pool.submit(thread_pool_fun,i)
            threads.append(obj)
        for i in range(gevent_num):
            obj = gevent.spawn(gevent_fun,i)
            gevents.append(obj)
        p_pool.shutdown()
        t_pool.shutdown()
        gevent.joinall(gevents)
        p_result = [item.result() for item in processs]
        t_result = [item.result() for item in threads]
        g_value = [item.value for item in gevents]
        print('<p_result:%s>'%p_result)
        print('<t_result:%s>'%t_result)
        print('<g_value:%s>'%g_value)
調用單純開啟程序、線程、協成的方式
    thread(5)
    process(5)
    gev(5)
    thread_pool(5)
    process_pool(5)
    pool(5)
調用混合開啟程序、線程、協成的方式
    run(5,5,5)      

本文來自部落格園,作者:前方、有光,轉載請注明原文連結:https://www.cnblogs.com/52-qq/p/8282416.html