在带有多处理/多线程的python中使用新的current.futures库时。我如何暴露错误以便显示出来。
我故意使用一个不存在的变量向get_user_object
中引入了一个错误。我希望这里会出现错误,但是程序会立即挂起/完成。
import concurrent.futures
import multiprocessing as mp
def get_user_object(batch, doesent_exist):
with _COUNTER.get_lock():
_COUNTER.value += 1
print(_COUNTER.value, end=' ')
#error here
print(variable_that_doesent_exist)
def init_globals(counter):
global _COUNTER
_COUNTER = counter
def main():
counter = mp.Value('i', 0)
with concurrent.futures.ProcessPoolExecutor(
initializer=init_globals, initargs=(counter,)
) as executor:
for _ in executor.map(get_user_object, range(10)):
pass
print()
if __name__ == "__main__":
import sys
sys.exit(main())