python多进程中使用pool
2017-01-14 12:33
162 查看
Python 多进程中使用pool,pool中指定每次运行几个进程,当其中一个进程结束完毕后,会加入新的进程
[python] view
plain copy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/9e12f1d3e499fc949c886e7c9e0484f9)
#!/usr/bin/env python
#coding: utf-8
import multiprocessing
import os,time,random
def Lee():
print "Run 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 "Run 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 "Run 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 "Run 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) #Pool执行函数,apply执行函数,当有一个进程执行完毕后,会添加一个新的进程到pool
bb8c
中
print 'Waiting for all subprocesses done...'
pool.close()
pool.join() #调用join之前,一定要先调用close() 函数,否则会出错, close()执行后不会有新的进程加入到pool,join函数等待素有子进程结束
print 'All subprocesses done.'
[python] view
plain copy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
#!/usr/bin/env python
#coding: utf-8
import multiprocessing
import os,time,random
def Lee():
print "Run 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 "Run 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 "Run 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 "Run 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) #Pool执行函数,apply执行函数,当有一个进程执行完毕后,会添加一个新的进程到pool
bb8c
中
print 'Waiting for all subprocesses done...'
pool.close()
pool.join() #调用join之前,一定要先调用close() 函数,否则会出错, close()执行后不会有新的进程加入到pool,join函数等待素有子进程结束
print 'All subprocesses done.'
相关文章推荐
- python多进程中使用pool
- python threadpool使用注意事项
- python 进程池1 - Pool使用简介
- python线程池(threadpool)模块使用
- python使用多进程或者多线程
- Python多进程multiprocessing使用示例
- Python使用多进程(核)外壳
- python中多进程+协程的使用以及为什么要用它
- 为什么在Python里推荐使用多进程而不是多线程?
- python线程池(threadpool)模块使用
- python多进程multiprocessing的简单使用
- Python学习笔记(四)多进程的使用
- python 使用Pool,并需要关注结果
- [Python]django使用多进程连接msyql错误
- Python中的多进程与多线程/分布式该如何使用
- 转载:浅析Python中的多进程与多线程的使用
- python线程池(threadpool)模块使用
- [Python]django使用多进程连接msyql错误
- Python多进程不要使用TimedRotatingFileHandler
- Python多进程multiprocessing使用示例