2010-02-11 43 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