Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to pass None as a timeout value to disable timeout logic #834

Merged
merged 2 commits into from
Mar 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions aiohttp/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ class Timeout:
... await r.text()


:param timeout: timeout value in seconds
:param timeout: timeout value in seconds or None to disable timeout logic
:param loop: asyncio compatible event loop
"""
def __init__(self, timeout, *, loop=None):
Expand All @@ -477,17 +477,19 @@ def __enter__(self):
if self._task is None:
raise RuntimeError('Timeout context manager should be used '
'inside a task')
self._cancel_handler = self._loop.call_later(
self._timeout, self._cancel_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 asyncio.CancelledError and self._cancelled:
self._cancel_handler = None
self._task = None
raise asyncio.TimeoutError
self._cancel_handler.cancel()
self._cancel_handler = None
if self._timeout is not None:
self._cancel_handler.cancel()
self._cancel_handler = None
self._task = None

def _cancel_task(self):
Expand Down
12 changes: 12 additions & 0 deletions tests/test_timeout.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ def run():
loop.run_until_complete(run())


@pytest.mark.run_loop
def test_timeout_disable(loop):
@asyncio.coroutine
def long_running_task():
yield from asyncio.sleep(0.1, loop=loop)
return 'done'

with Timeout(None, loop=loop):
resp = yield from long_running_task()
assert resp == 'done'


@pytest.mark.run_loop
def test_timeout_not_relevant_exception(loop):
yield from asyncio.sleep(0, loop=loop)
Expand Down