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)