【发布时间】:2018-08-25 14:57:21
【问题描述】:
我正在尝试在 Windows 7 上的 Python 3.6 中使用 ThreadPoolExecutor,但似乎异常会被静默忽略或停止程序执行。示例代码:
#!/usr/bin/env python3
from time import sleep
from concurrent.futures import ThreadPoolExecutor
EXECUTOR = ThreadPoolExecutor(2)
def run_jobs():
EXECUTOR.submit(some_long_task1)
EXECUTOR.submit(some_long_task2, 'hello', 123)
return 'Two jobs was launched in background!'
def some_long_task1():
print("Task #1 started!")
for i in range(10000000):
j = i + 1
1/0
print("Task #1 is done!")
def some_long_task2(arg1, arg2):
print("Task #2 started with args: %s %s!" % (arg1, arg2))
for i in range(10000000):
j = i + 1
print("Task #2 is done!")
if __name__ == '__main__':
run_jobs()
while True:
sleep(1)
输出:
Task #1 started!
Task #2 started with args: hello 123!
Task #2 is done!
它一直挂在那里,直到我用 Ctrl+C 杀死它。
但是,当我从 some_long_task1 中删除 1/0 时,任务 #1 会毫无问题地完成:
Task #1 started!
Task #2 started with args: hello 123!
Task #1 is done!
Task #2 is done!
我需要捕获在ThreadPoolExecutor 中运行的函数中引发的异常不知何故。
Python 3.6 (Minconda),Windows 7 x64。
【问题讨论】:
标签: python python-3.x windows-7-x64 python-multithreading cpython