-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Timeout context manager #611
Conversation
interesting idea, I see this more in asyncio itself than aiohttp. |
Nice!
However, I have bad feelings that this context manager wouldn't work correctly for the tasks that are spawned from the timeout block:
I was expected to see an exception, or |
Looks like we should make the same guarantees for |
I ported tests from async with Timeout(None):
t = await future in this case timeout is not calculated, similar to |
as result of discussion in IRL:
|
:param raise_error: if set, TimeoutError is raised in case of timeout | ||
:param loop: asyncio compatible event loop | ||
""" | ||
def __init__(self, timeout, *, raise_error=False, loop=None): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
raise_error
should be True
by default (asyncio.wait_for()
raises exception on timeout).
After thinking I doubt do we need the parameter at all.
raise_error argument removed |
self._cancel_handler.cancel() | ||
|
||
def _cancel_task(self): | ||
self._cancelled = self._task.cancel() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add self._task = None
@asvetlov I implemented your idea about timeout context manager.
Open questions:
with Timeout(1):
insteadasync with Timeout(1)?:
Edit: Add related stackoverflow question:
http://stackoverflow.com/questions/33217429/creating-a-temporary-async-timer-callback-to-a-bound-method-with-python-asyncio