process_进程池_2

import multiprocessing
import os, time, random

def Lee():
print("\nRun task Lee-%s" %(os.getpid()) ) #os.getpid()获取当前的进程的ID
start = time.time()
time.sleep(random.random() * 10) #random.random()随机生成0-1之间的小数
end = time.time()
print('Task Lee, runs %0.2f seconds.' %(end - start))

def Marlon():
print("\nRun task Marlon-%s" %(os.getpid()))
start = time.time()
time.sleep(random.random() * 40)
end=time.time()
print('Task Marlon runs %0.2f seconds.' %(end - start))

def Allen():
print("\nRun task Allen-%s" %(os.getpid()))
start = time.time()
time.sleep(random.random() * 30)
end = time.time()
print( 'Task Allen runs %0.2f seconds.' %(end - start))

def Frank():
print("\nRun task Frank-%s" %(os.getpid()))
start = time.time()
time.sleep(random.random() * 20)
end = time.time()
print( 'Task Frank runs %0.2f seconds.' %(end - start))
if __name__=='__main__':
function_list= [Lee, Marlon, Allen, Frank]
print( "parent process %s" %(os.getpid()))

pool=multiprocessing.Pool(2)
for func in function_list:
pool.apply_async(func)

print('Waiting for all subprocesses done...')
pool.close()
pool.join()
print('All subprocesses done.')
'''
parent process 3240
Waiting for all subprocesses done...

Run task Lee-13652

Run task Marlon-13528
Task Lee, runs 3.34 seconds.

Run task Allen-13652
Task Allen runs 14.78 seconds.

Run task Frank-13652
Task Frank runs 2.45 seconds.
Task Marlon runs 32.62 seconds.
All subprocesses done.
'''

转载于:https://www.cnblogs.com/hapyygril/p/9836898.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值