# coding=utf-8
import time
import random
from multiprocessing.dummy import Pool as ThreadPool
def to_reve_function(p1,):
print 'p1::: '+str(p1)+' start==== '
time.sleep(random.randint(1,4))
print ':::p1 '+str(p1)+' end===='
return p1
# print time.clock()
threadQty = 10 # 进程池大小
async_pool = ThreadPool(processes=threadQty)
#t = time.clock()
datas = []
for i in range(0,8):
result = async_pool.apply_async(to_reve_function, (i,)) # 进程池调用函数
datas.append(result)
for data in datas:
data.wait()
for i in datas:
if i.ready() and i.successful():
restat = i.get()
print 'restat::: '+str(restat)
---------------------------
执行结果
p1::: 0 start====
p1::: 1 start====
p1::: 2 start====
p1::: 3 start====
p1::: 4 start====
p1::: 5 start====
p1::: 6 start====
p1::: 7 start====
:::p1 2 end====
:::p1 5 end====
:::p1 1 end====
:::p1 3 end====
:::p1 6 end====
:::p1 0 end====
:::p1 4 end====
:::p1 7 end====
restat::: 0
restat::: 1
restat::: 2
restat::: 3
restat::: 4
restat::: 5
restat::: 6
restat::: 7
---------------------------
Python 进程池的使用
最新推荐文章于 2023-08-12 02:06:22 发布