關於python程序池先close再join的疑惑
阿新 • • 發佈:2019-02-02
import time
from multiprocessing import Pool
def run(fn):
#fn: 函式引數是資料列表的一個元素
time.sleep(1)
return fn*fn
if __name__ == "__main__":
testFL = [1,2,3,4,5,6]
print 'shunxu:' #順序執行(也就是序列執行,單程序)
s = time.time()
for fn in testFL:
run(fn)
e1 = time.time()
print "順序執行時間:", int(e1 - s)
print 'concurrent:' #建立多個程序,並行執行
pool = Pool(5) #建立擁有5個程序數量的程序池
#testFL:要處理的資料列表,run:處理testFL列表中資料的函式
rl =pool.map(run, testFL)
pool.close()#關閉程序池,不再接受新的程序
pool.join()#主程序阻塞等待子程序的退出
e2 = time.time()
print "並行執行時間:", int(e2-e1)
print rl
def close(self):
debug('closing pool')
if self._state == RUN:
self._state = CLOSE
self._worker_handler._state = CLOSE
由上可知,當程序池close的時候並未關閉程序池,只是會把狀態改為不可再插入元素的狀態,完全關閉程序池使用
def _terminate_pool(cls, taskqueue, inqueue, outqueue, pool,
worker_handler, task_handler, result_handler, cache):
# this is guaranteed to only be called once
debug('finalizing pool')
worker_handler._state = TERMINATE
task_handler._state = TERMINATE
debug('helping task handler/workers to finish')
cls._help_stuff_finish(inqueue, task_handler, len(pool))
assert result_handler.is_alive() or len(cache) == 0
result_handler._state = TERMINATE
outqueue.put(None) # sentinel
# We must wait for the worker handler to exit before terminating
# workers because we don't want workers to be restarted behind our back.
debug('joining worker handler')
if threading.current_thread() is not worker_handler:
worker_handler.join(1e100)
# Terminate workers which haven't already finished.
if pool and hasattr(pool[0], 'terminate'):
debug('terminating workers')
for p in pool:
if p.exitcode is None:
p.terminate()
debug('joining task handler')
if threading.current_thread() is not task_handler:
task_handler.join(1e100)
debug('joining result handler')
if threading.current_thread() is not result_handler:
result_handler.join(1e100)
if pool and hasattr(pool[0], 'terminate'):
debug('joining pool workers')
for p in pool:
if p.is_alive():
# worker has not yet exited
debug('cleaning up worker %d' % p.pid)
p.join()