summaryrefslogtreecommitdiffstats
path: root/Lib/asyncio
diff options
context:
space:
mode:
authorYury Selivanov <yury@magic.io>2016-06-08 17:57:23 (GMT)
committerYury Selivanov <yury@magic.io>2016-06-08 17:57:23 (GMT)
commit7a713386d84854f72731f178f1bdacc0575cd422 (patch)
treeb31ec185ccee0cfcda426e5cda6454fbe07dfabb /Lib/asyncio
parentd211bfbe79c34edc09f340e626540d5a3cb1d48a (diff)
parentc1cf296de6d2dc18c6d692c9eb0e5c5c278fb938 (diff)
downloadcpython-7a713386d84854f72731f178f1bdacc0575cd422.zip
cpython-7a713386d84854f72731f178f1bdacc0575cd422.tar.gz
cpython-7a713386d84854f72731f178f1bdacc0575cd422.tar.bz2
Merge 3.5 (asyncio)
Diffstat (limited to 'Lib/asyncio')
-rw-r--r--Lib/asyncio/tasks.py53
1 files changed, 0 insertions, 53 deletions
diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py
index 81510ba..0cca8e3 100644
--- a/Lib/asyncio/tasks.py
+++ b/Lib/asyncio/tasks.py
@@ -4,7 +4,6 @@ __all__ = ['Task',
'FIRST_COMPLETED', 'FIRST_EXCEPTION', 'ALL_COMPLETED',
'wait', 'wait_for', 'as_completed', 'sleep', 'async',
'gather', 'shield', 'ensure_future', 'run_coroutine_threadsafe',
- 'timeout',
]
import concurrent.futures
@@ -737,55 +736,3 @@ def run_coroutine_threadsafe(coro, loop):
loop.call_soon_threadsafe(callback)
return future
-
-
-def timeout(timeout, *, loop=None):
- """A factory which produce a context manager with timeout.
-
- Useful in cases when you want to apply timeout logic around block
- of code or in cases when asyncio.wait_for is not suitable.
-
- For example:
-
- >>> with asyncio.timeout(0.001):
- ... yield from coro()
-
-
- timeout: timeout value in seconds or None to disable timeout logic
- loop: asyncio compatible event loop
- """
- if loop is None:
- loop = events.get_event_loop()
- return _Timeout(timeout, loop=loop)
-
-
-class _Timeout:
- def __init__(self, timeout, *, loop):
- self._timeout = timeout
- self._loop = loop
- self._task = None
- self._cancelled = False
- self._cancel_handler = None
-
- def __enter__(self):
- self._task = Task.current_task(loop=self._loop)
- if self._task is None:
- raise RuntimeError('Timeout context manager should be used '
- 'inside a task')
- if self._timeout is not None:
- self._cancel_handler = self._loop.call_later(
- self._timeout, self._cancel_task)
- return self
-
- def __exit__(self, exc_type, exc_val, exc_tb):
- if exc_type is futures.CancelledError and self._cancelled:
- self._cancel_handler = None
- self._task = None
- raise futures.TimeoutError
- if self._timeout is not None:
- self._cancel_handler.cancel()
- self._cancel_handler = None
- self._task = None
-
- def _cancel_task(self):
- self._cancelled = self._task.cancel()