如何处理来自 Python 个多个池的所有错误?
How to handle all errors from Python multiple pools?
为了演示问题我准备了简单的代码:
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
在这个特定的示例中,作为输出,我得到:
first
second
'Error msg: first'
但我更需要:
first
second
'Error msg: first'
'Error msg: second'
问题在于,在主函数级别上,我尝试访问所有唯一的错误消息,而不是在 pool_function 级别上处理它,但我只收到第一个错误消息。不幸的是,我使用的实际代码要复杂得多,因此很难使用示例中演示的这段代码的结构来做一些事情。我需要一些干净直接的解决方案来获取所有错误消息并在主函数级别处理它。
感谢您提供任何解决方案的建议。
您必须将 try~ except
放入 pool_function
而不是 __main__
。如果否,__main__
将在第一个之后停止,除了被提升并且没有机会让第二个到 运行。这是您正在尝试的内容:
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
它有效,但似乎不是一个好方法,所以:
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
为了演示问题我准备了简单的代码:
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
在这个特定的示例中,作为输出,我得到:
first
second
'Error msg: first'
但我更需要:
first
second
'Error msg: first'
'Error msg: second'
问题在于,在主函数级别上,我尝试访问所有唯一的错误消息,而不是在 pool_function 级别上处理它,但我只收到第一个错误消息。不幸的是,我使用的实际代码要复杂得多,因此很难使用示例中演示的这段代码的结构来做一些事情。我需要一些干净直接的解决方案来获取所有错误消息并在主函数级别处理它。
感谢您提供任何解决方案的建议。
您必须将 try~ except
放入 pool_function
而不是 __main__
。如果否,__main__
将在第一个之后停止,除了被提升并且没有机会让第二个到 运行。这是您正在尝试的内容:
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
它有效,但似乎不是一个好方法,所以:
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