from multiprocessing
import Pool
import os
import time
import random
def long_time_task(name):
print(
'Run task %s (%s)...' % (name, os.getpid()))
start =time.time()
time.sleep(random.random()*
3)
end =time.time()
print(
'Task runs %0.2f seconds.' % (name, (end - start)))
if __name__ ==
'__main__':
print(
'Parent process %s. ' % os.getpid())
p = Pool(
4)
for i
in range(
5):
p.apply_async(long_time_task,args=(i,))
print(
'Waiting for all subprocess done...')
p.close()
p.join()
print(
'All suprocess done.')
运行结果
转载于:https://www.cnblogs.com/bilaisheng/p/10211017.html
相关资源:JAVA上百实例源码以及开源项目