2010-02-11 45 views
6

我是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() 

但仍然等待,不知道什么是缺失。

+0

看起来像ConfigParser与SQLAlchemy有相同的问题(异常不可pickleable),请参阅[在SQL脚本中使用SQLAlchemy和多处理挂起](http://stackoverflow.com/questions/8785899/hang-in-python-script - 使用 - SQLAlchemy的和多处理)。我报告过这个问题,因为[ConfigParser异常不可选](http://bugs.python.org/issue13760)。 – 2012-01-11 07:56:32

回答

0

我有同样的问题。当工作进程引发具有自定义构造函数的用户异常时,会发生这种情况。确保您的例外(ConfigParser.NoOptionError在这种情况下)使用的基本异常准确两个参数:

class NoOptionError(ValueError): 

    def __init__(self, message, *args): 
     super(NoOptionError, self).__init__(message, args) 
+1

这个异常来自Python模块(ConfigParser),需要在那里修复。 – 2012-01-11 07:43:20

2

在Python 3.2+此按预期工作。对于Python 2,这个bug在r74545中得到了修复,并将在Python 2.7.3中提供。与此同时,您可以使用configparser库,该库是3.2+的configparser的backport。 Check it out.

+0

这是一个在configparser或多进程的大吗? (我得到与多进程相同的错误,与configparser无关) – user48956 2017-10-11 22:00:37