本文介绍了为什么在concurrent.futures.Future实例中不会引发TimeoutError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我来自 https://的示例docs.python.org/3/library/concurrent.futures.html#id1 。 我更新了以下内容: data = future.result() 到此: data = future.result(timeout = 0.1) I've update the following:data = future.result()to this:data = future.result(timeout=0.1) concurrent.futures.Future.result 的文档陈述如下: 如果调用没有在超时秒内完成,则会引发TimeoutError。 timeout可以是一个int或float If the call hasn’t completed in timeout seconds, then a TimeoutError will be raised. timeout can be an int or float (我知道请求有一个超时,60,真正的代码我执行不同的操作,不使用urllib请求) import concurrent.futuresimport urllib.requestURLS = ['http://www.foxnews.com/', 'http://www.cnn.com/', 'http://europe.wsj.com/', 'http://www.bbc.co.uk/', 'http://some-made-up-domain.com/']# Retrieve a single page and report the url and contentsdef load_url(url, timeout): conn = urllib.request.urlopen(url, timeout=timeout) return conn.readall()# We can use a with statement to ensure threads are cleaned up promptlywith 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: # The below timeout isn't raising the TimeoutError. data = future.result(timeout=0.01) except Exception as exc: print('%r generated an exception: %s' % (url, exc)) else: print('%r page is %d bytes' % (url, len(data))) 如果我在调用 as_completed 时设置它,则会引发b $ b TimeoutError TimeoutError is raised if I set it on the call to as_completed, but I need to set the timeout on a per Future basis, not all of them as a whole.感谢@jme,使用单个Future,但不是使用下面的多个。我需要 yield 在函数开始时允许 futures dict的构建吗?从文档听起来像对提交的调用不应该阻止。 Thanks @jme, that works with a single Future, but not with multiples using the below. Do I need to yield at the beginning of the functions to allow the build-up of the futures dict? From the docs it sounds like the calls to submit shouldn't block. import concurrent.futuresimport timeimport sysdef wait(): time.sleep(5) return 42with concurrent.futures.ThreadPoolExecutor(4) as executor: waits = [wait, wait] futures = {executor.submit(w): w for w in waits} for future in concurrent.futures.as_completed(futures): try: future.result(timeout=1) except concurrent.futures.TimeoutError: print("Too long!") sys.stdout.flush()print(future.result()) 推荐答案问题似乎是调用 concurrent.futures.as_completed()。 如果我用代替循环,一切似乎都有效:If I replace that with just a for loop, everything seems to work: for wait, future in [(w, executor.submit(w)) for w in waits]: ...我错误地解释了 as_completed 的文档:I misinterpreted the doc for as_completed which states: ...在完成(完成或已取消)时产生期货... ...yields futures as they complete (finished or were cancelled)... as_completed 将处理超时,但作为一个整体,不是每个未来。as_completed will handle timeouts but as a whole, not on a per future basis. 这篇关于为什么在concurrent.futures.Future实例中不会引发TimeoutError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-18 01:30