def test():
a=[2]
print(a[3])
def run():
test()
def err(error):
logger.exception(error)
if __name__ == '__main__':
pool = Pool(2)
t1 = pool.apply_async(run,error_callback=err)
pool.close()
pool.join()
日志记录 :
list index out of range
- 显然并没有拿到堆栈信息,在 callback 里再加上 try
def test():
a=[2]
print(a[3])
def run():
test()
def err(error):
#这边已经拿到 error 了但是还要 try,要不然日志只记录 list index out of range
try:
raise error
except Exception as e:
logger.exception(e)
if __name__ == '__main__':
pool = Pool(2)
t1 = pool.apply_async(run,error_callback=err)
pool.close()
pool.join()
日志输出 :
list index out of range
multiprocessing.pool.RemoteTraceback:
"""
Traceback (most recent call last):
File "/usr/lib/python3.5/multiprocessing/pool.py", line 119, in worker
result = (True, func(*args, **kwds))
File "/home/work_dir/test/timer.py", line 13, in run
test()
File "/home/work_dir/test/timer.py", line 10, in test
print(a[3])
IndexError: list index out of range
"""
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/work_dir/test/timer.py", line 17, in err
raise error
IndexError: list index out of range