我是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()
但仍然等待,不知道什么是缺失。
看起来像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