gpt4 book ai didi

python - 当任何线程中出现异常时,多处理池挂起

转载 作者:太空狗 更新时间:2023-10-29 21:57:36 25 4
gpt4 key购买 nike

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

但它仍在等待,不确定缺少什么。

最佳答案

在 Python 3.2+ 中,这按预期工作。对于 Python 2,此错误已在 r74545 中修复,并将在 Python 2.7.3 中可用。同时,您可以使用 configparser 库,它是 3.2+ 的 configparser 的反向端口。 Check it out.

关于python - 当任何线程中出现异常时,多处理池挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2246384/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com