如何解决concurrent.futures.ThreadPoolExecutor:KeyboardInterrupt 不会触发 Windows 10 上的应用程序退出
我注意到 concurrent.futures.ThreadPoolExecutor
在 Darwin OS 上在被 KeyboardInterrupt
中断时干净地退出应用程序方面效果很好。但是,Windows 10 并非如此,该进程会挂起数分钟,而没有任何指示抛出异常。
这是一个示例代码:
import concurrent.futures
import urllib.request
URLS = ['http://www.foxnews.com/','http://www.cnn.com/','http://europe.wsj.com/','http://www.bbc.co.uk/','http://some-made-up-domain.com/']
URLS *= 100
# Retrieve a single page and report the URL and contents
def load_url(url,timeout):
with urllib.request.urlopen(url,timeout=timeout) as conn:
return conn.read()
# We can use a with statement to ensure threads are cleaned up promptly
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
# Start the load operations and mark each future with its URL
future_to_url = {executor.submit(load_url,url,60): url for url in URLS}
for future in concurrent.futures.as_completed(future_to_url):
url = future_to_url[future]
try:
data = future.result()
except Exception as exc:
print('%r generated an exception: %s' % (url,exc))
else:
print('%r page is %d bytes' % (url,len(data)))
我不确定我是否错过了超时 kwarg 或类似的东西。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。