多进程池在某个线程出现异常时挂起
我刚接触Python,正在尝试用multiprocessing.pool来处理文件,只要没有出现异常,程序运行得很好。如果有任何一个线程或进程出现异常,整个程序就会等待那个线程。
以下是代码片段:
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'",))
我继续添加了一个异常处理器来终止进程。
try:
ifile=cp.get(table,'inputfilename')
except ConfigParser.NoSectionError,ConfigParser.NoOptionError:
usage("One of Parameter not found for"+ table)
terminate()
但程序还是在等待,不知道缺少了什么。
2 个回答
0
我也遇到过同样的问题。这种情况发生在一个工作进程抛出了一个用户自定义的异常,而这个异常有一个特别的构造函数。确保你的异常(在这个例子中是ConfigParser.NoOptionError)在初始化时,传递给基础异常的参数必须是正好两个:
class NoOptionError(ValueError):
def __init__(self, message, *args):
super(NoOptionError, self).__init__(message, args)
2
在Python 3.2及以上版本,这个问题的表现是正常的。对于Python 2,这个错误在版本r74545中被修复,并将在Python 2.7.3中提供。在此之前,你可以使用configparser
库,这个库是从3.2+版本移植过来的。可以去看看。