多进程在开发时,信息无法print,或者子进程根本就开启不起来,输出窗口也无法看到异常信息。这时使用error_callback回调,是不二法门。
开启进程时,增加error_callback方法,程序异常,直接执行回调函数。错误一幕了然
简单示例:
def helloworld(x):
print(x / 0)
return x + 1
if __name__ == '__main__':
arr = list(range(10))
with multiprocessing.Pool(min(os.cpu_count(), math.ceil(len(arr) * 1.0 / 2), 4)) as pool:
results = []
for t in arr:
logging.info(t)
r = pool.apply_async(func=helloworld,
args=(t,), # 单个参数时,要构成元组,逗号不能丢
error_callback=lambda x: logging.exception(f'func failed:{x}')
)
results.append(r)
sum = 0
for r in results:
sum += r.get()
print(sum)