任何线程中有异常时,多处理池都将挂起 [英] multiprocessing Pool hangs when there is a exception in any of the thread
问题描述
我是Python的新手,正在尝试使用multiprocessing.pool程序来处理文件,只要没有异常,它就可以正常工作.如果任何线程/进程获得异常,则整个程序将等待线程
I am new to Python and trying a multiprocessing.pool program to process files, it works fine as long as there are no exceptions. If any of the thread/process gets an exception the whole program waits for the thread
代码段:
cp = ConfigParser.ConfigParser()
cp.read(gdbini)
for table in cp.sections():
jobs.append(table)
#print jobs
poolreturn = pool.map(worker, jobs)
pool.close()
pool.join()
失败消息:
Traceback (most recent call last):
File "/opt/cnet-python/default-2.6/lib/python2.6/threading.py", line 525, in __bootstrap_inner
self.run()
File "/opt/cnet-python/default-2.6/lib/python2.6/threading.py", line 477, in run
self.__target(*self.__args, **self.__kwargs)
File "/opt/cnet-python/default-2.6/lib/python2.6/multiprocessing/pool.py", line 259, in _handle_results
task = get()
TypeError: ('__init__() takes exactly 3 arguments (2 given)', <class 'ConfigParser.NoOptionError'>, ("No option 'inputfilename' in section: 'section-1'",))
我继续添加了一个异常处理程序来终止进程
I went ahead added a exception handler to terminate the process
try:
ifile=cp.get(table,'inputfilename')
except ConfigParser.NoSectionError,ConfigParser.NoOptionError:
usage("One of Parameter not found for"+ table)
terminate()
但是它仍然在等待,不确定丢失了什么.
but still it waits, not sure whats missing.
推荐答案
在Python 3.2及更高版本中,此方法可以正常工作.对于Python 2,此错误已在r74545中修复,并将在Python 2.7.3中提供.同时,您可以使用configparser
库,该库是3.2+起的configparser的反向端口. 查看.
In Python 3.2+ this works as expected. For Python 2, this bug was fixed in r74545 and will be available in Python 2.7.3. In the mean time, you can use the configparser
library which is a backport of the configparser from 3.2+. Check it out.
这篇关于任何线程中有异常时,多处理池都将挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!