summaryrefslogtreecommitdiffstats
path: root/Lib/asyncio/tasks.py
diff options
context:
space:
mode:
authorVictor K <hellysmile@gmail.com>2017-10-05 16:04:39 (GMT)
committerYury Selivanov <yury@magic.io>2017-10-05 16:04:39 (GMT)
commit4d071897880b7e84e1a217ebe19971c118316970 (patch)
tree51656b5e3e40004a2a8d4251ebdb013723bbce54 /Lib/asyncio/tasks.py
parent11045c9d8a21dd9bd182a3939189db02815f9783 (diff)
downloadcpython-4d071897880b7e84e1a217ebe19971c118316970.zip
cpython-4d071897880b7e84e1a217ebe19971c118316970.tar.gz
cpython-4d071897880b7e84e1a217ebe19971c118316970.tar.bz2
bpo-31556: asyncio.wait_for can cancel futures faster with timeout <= 0 (#3703)
Diffstat (limited to 'Lib/asyncio/tasks.py')
-rw-r--r--Lib/asyncio/tasks.py9
1 files changed, 9 insertions, 0 deletions
diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py
index 575d205..52fef18 100644
--- a/Lib/asyncio/tasks.py
+++ b/Lib/asyncio/tasks.py
@@ -334,6 +334,15 @@ def wait_for(fut, timeout, *, loop=None):
if timeout is None:
return (yield from fut)
+ if timeout <= 0:
+ fut = ensure_future(fut, loop=loop)
+
+ if fut.done():
+ return fut.result()
+
+ fut.cancel()
+ raise futures.TimeoutError()
+
waiter = loop.create_future()
timeout_handle = loop.call_later(timeout, _release_waiter, waiter)
cb = functools.partial(_release_waiter, waiter)