多進程

multiprocessing的pool的進程池裏有多進程池和多進程池,分別不同的引用:
多進程:

    import multiprocessing import Pool 
    p = Pool(processes=3processes=3)

多線程:

    from multiprocessing.dummy import Pool as ThreadPool 
    p = ThreadPool(processes=3)

# -*- coding: utf-8 -*- 

from multiprocessing import Pool 
from multiprocessing.dummy import Pool as ThreadPool 
import time 

def fun(msg): 
        print('msg: ', msg) 
        time.sleep(1) 
        print('********') 
        return 'fun_return %s' % msg 

            # apply_async 
print('\n------apply_async-------') 
# async_pool = Pool(processes=4)   #多進程
async_pool = ThreadPool(processes=4)   #多線程
results =[] 
for i in range(5): 
        msg = 'msg: %d' % i 
        result = async_pool.apply_async(fun, (msg, )) 
        results.append(result) 
        print('apply_async: 不堵塞') 
        # async_pool.close()     #再往池裏添加
        # async_pool.join()        #等待池裏的進程或線程都執行完

        for i in results: 
                i.wait() # 等待線程函數執行完畢        #等待單個進程或線程執行完

        for i in results: 
                if i.ready(): # 線程函數是否已經啓動了 
                        if i.successful(): # 線程函數是否執行成功 
                                print(i.get()) # 線程函數返回值 

# apply 
print('\n------apply-------') 
pool = ThreadPool(processes=4) 
results =[] 
for i in range(5): 
        msg = 'msg: %d' % i 
        result = pool.apply(fun, (msg, )) 
        results.append(result) 
        print('apply: 堵塞') 
        print(results)

# map_async 
print('\n------map_async-------') 
arg = [1, 2, 10, 11, 18] 
async_pool = ThreadPool(processes=4)
result = async_pool.map_async(fun, arg) 
print(result.ready()) # 線程函數是否已經啓動了 
print('map_async: 不堵塞') 
result.wait() # 等待所有線程函數執行完畢 
print('after wait') 
if result.ready(): # 線程函數是否已經啓動了 
        if result.successful(): # 線程函數是否執行成功 
                print(result.get()) # 線程函數返回值 

# map 
print('\n------map-------') 
arg = [3, 5, 11, 19, 12] 
pool = ThreadPool(processes=3) 
return_list = pool.map(fun, arg) 
print('map: 堵塞') 
pool.close() 
pool.join() 
print(return_list) 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章