python 多线程笔记
2017-06-21 本文已影响11人
camlboy
while True:
line = fileInfo.readline()
if line:
threadPool.submit(task,line,q,noFile,okFile)
else:
threadPool.shutdown()
isStop = True
noFile.close()
okFile.close()
fileInfo.close()
break
如上代码,线程池提交任务,默认读到的line为空时退出while循环,此时主线程终止,程序退出,调用shutDown()方法等待线程池中的任务执行完再退出程序,shutDown源码如下,默认等待,可以设置为不等待
def shutdown(self, wait=True):
with self._shutdown_lock:
self._shutdown = True
self._work_queue.put(None)
if wait:
for t in self._threads:
t.join(sys.maxint)