[英]How to handle all errors from Python multiple pools?
to demonstrate problem I've prepared simple code: 为了演示问题,我准备了简单的代码:
from multiprocessing import Pool
class MyError(Exception):
def __str__(self):
return repr("Error msg: " + self.args[0])
def pool_function(msg):
print msg
raise MyError(msg)
return 0
def some_function():
my_pool = Pool(2)
msg = ['first', 'second']
my_pool.map(pool_function, msg)
if __name__ == '__main__':
try:
some_function()
except MyError, msg:
print msg
In this particular example as output I get: 在此特定示例中,我得到:
first
second
'Error msg: first'
but I need rather: 但我需要:
first
second
'Error msg: first'
'Error msg: second'
Problem is that on the level of the main function I try to get access to all error messages which are unique, rather than handling it on level of pool_function but I get only first error msg. 问题是,在main函数的级别上,我尝试访问所有唯一的错误消息,而不是在pool_function级别上对其进行处理,但是我仅收到第一个错误消息。 Unfortunately the real code with which I work is much more complex so is rather hard to do something with the structure of this code which is demonstrated in example. 不幸的是,我使用的实际代码要复杂得多,因此很难用示例中演示的代码结构来做一些事情。 I need some clean and straight forward solution to get all error messages and process it on the level of main function. 我需要一些干净直接的解决方案来获取所有错误消息并在主功能级别上对其进行处理。
Thanks in advice for any solutions. 感谢您提供任何解决方案的建议。
You have to put try~ except
in your pool_function
not __main__
. 你必须把try~ except
你pool_function
不__main__
。 If no, __main__
will stop after the first except raised and left no chance for the second one to run. 如果否,则__main__
将在第一个运行之后停止运行,除非第二个运行。 This is following what you are trying: 这是您正在尝试的:
def pool_function(msg):
print msg
try:
raise MyError(msg)
except:
return MyError(msg)
def some_function():
my_pool = Pool(2)
msg = ['first', 'second']
return my_pool.map(pool_function, msg)
if __name__ == '__main__':
try:
msg= some_function()
except MyError, msg:
print msg
It works, but seem not a good way, so: 它有效,但似乎不是一个好方法,因此:
def pool_function(msg):
print msg
try:
# do something
raise MyError(msg)
except:
return 0,MyError(msg)
else:
return 1,# some result
def some_function():
my_pool = Pool(2)
msg = ['first', 'second']
return return my_pool.map(pool_function, msg)
if __name__ == '__main__':
msg = some_function()
for result in msg:
if result[0]:
# do something when it run successfully
elif not result[0]:
print result[1]
# do something when it got errors
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.