10000 bpo-46771: Implement asyncio context managers for handling timeouts by asvetlov · Pull Request #31394 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-46771: Implement asyncio context managers for handling timeouts #31394

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

Merged
merged 31 commits into from
Mar 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
dac5874
bpo-46771: Implement asyncio context managers for handling timeouts
asvetlov Mar 2, 2022
374ff2a
Add reschedule tests
asvetlov Mar 2, 2022
70fa59b
Add reschedule tests
asvetlov Mar 2, 2022
3ae2af6
Dro breakpoint
asvetlov Mar 2, 2022
1654ec4
Tune repr
asvetlov Mar 2, 2022
2c9dbf8
Add tests
asvetlov Mar 2, 2022
baa7400
More tests
asvetlov Mar 2, 2022
1ca0fb8
Rename
asvetlov Mar 2, 2022
8a81dd1
Update Lib/asyncio/timeouts.py
asvetlov Mar 8, 2022
fae235d
Update Lib/asyncio/timeouts.py
asvetlov Mar 8, 2022
663b82f
Update Lib/asyncio/timeouts.py
asvetlov Mar 8, 2022
24d62d1
Update Lib/asyncio/timeouts.py
asvetlov Mar 8, 2022
6a26d1b
Add a test
asvetlov Mar 8, 2022
930b92b
Polish docstrings
asvetlov Mar 8, 2022
ac5c53d
Format
asvetlov Mar 8, 2022
f96ad1c
Tune tests
asvetlov Mar 8, 2022 8000
94b4b4c
Fix comment
asvetlov Mar 8, 2022
388c6da
Tune comment
asvetlov Mar 8, 2022
cdc7f88
Tune docstrings
asvetlov Mar 8, 2022
9949fe4
Tune
asvetlov Mar 8, 2022
c716856
Tune tests
asvetlov Mar 8, 2022
b4889a0
Update Lib/test/test_asyncio/test_timeouts.py
asvetlov Mar 8, 2022
b6504e6
Tune tests
asvetlov Mar 8, 2022
fd2688d
Don't clobber foreign exceptions even if timeout is expiring
gvanrossum Mar 9, 2022
2ddda69
Add test from discussion
gvanrossum Mar 9, 2022
493545a
Fix indent of added test
gvanrossum Mar 9, 2022
ff36f2a
Disable slow callback warning
asvetlov Mar 9, 2022
8790e49
Reformat
asvetlov Mar 9, 2022
ac6f8c8
Increase delay
asvetlov Mar 9, 2022
e8c67ce
Don't raise TimeoutError if the CancelledError was swallowed by inner…
asvetlov Mar 9, 2022
e65d766
Don't duplicate py/c tests, timeout has no C accelerators
asvetlov Mar 10, 2022
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
Prev Previous commit
Next Next commit
Rename
  • Loading branch information
asvetlov committed Mar 9, 2022
commit 1ca0fb854ba6cf3bae0cd7b9571a42b65c447963
30 changes: 15 additions & 15 deletions Lib/asyncio/timeouts.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,34 @@ class _State(enum.Enum):
@final
class Timeout:

def __init__(self, deadline: Optional[float]) -> None:
def __init__(self, when: Optional[float]) -> None:
self._state = _State.CREATED

self._timeout_handler: Optional[events.TimerHandle] = None
self._task: Optional[tasks.Task[Any]] = None
self._deadline = deadline
self._when = when

def when(self) -> Optional[float]:
return self._deadline
return self._when

def reschedule(self, deadline: Optional[float]) -> None:
assert self._state != _State.CREATED
if self._state != _State.ENTERED:
def reschedule(self, when: Optional[float]) -> None:
assert self._state is not _State.CREATED
if self._state is not _State.ENTERED:
raise RuntimeError(
f"Cannot change state of {self._state} CancelScope",
f"Cannot change state of {self._state.value} Timeout",
)

self._deadline = deadline
self._when = when

if self._timeout_handler is not None:
self._timeout_handler.cancel()

if deadline is None:
if when is None:
self._timeout_handler = None
else:
loop = events.get_running_loop()
self._timeout_handler = loop.call_at(
deadline,
when,
self._on_timeout,
)

Expand All @@ -63,7 +63,7 @@ def expired(self) -> bool:
def __repr__(self) -> str:
info = ['']
if self._state is _State.ENTERED:
info.append(f"deadline={self._deadline:.3f}")
info.append(f"when={self._when:.3f}")
info_str = ' '.join(info)
return f"<Timeout [{self._state.value}]{info_str}>"

Expand All @@ -72,7 +72,7 @@ async def __aenter__(self) -> "Timeout":
self._task = tasks.current_task()
if self._task is None:
raise RuntimeError("Timeout should be used inside a task")
self.reschedule(self._deadline)
self.reschedule(self._when)
return self

async def __aexit__(
Expand Down Expand Up @@ -123,10 +123,10 @@ def timeout(delay: Optional[float]) -> Timeout:
return Timeout(loop.time() + delay if delay is not None else None)


def timeout_at(deadline: Optional[float]) -> Timeout:
def timeout_at(when: Optional[float]) -> Timeout:
"""Schedule the timeout at absolute time.

deadline argument points on the time in the same clock system
when argument points on the time in the same clock system
as loop.time().

Please note: it is not POSIX time but a time with
Expand All @@ -138,4 +138,4 @@ def timeout_at(deadline: Optional[float]) -> Timeout:


"""
return Timeout(deadline)
return Timeout(when)
2 changes: 1 addition & 1 deletion Lib/test/test_asyncio/test_timeouts.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ async def f():

async def test_repr_active(self):
async with asyncio.timeout(10) as cm:
self.assertRegex(repr(cm), r"<Timeout \[active\] deadline=\d+\.\d*>")
self.assertRegex(repr(cm), r"<Timeout \[active\] when=\d+\.\d*>")

async def test_repr_expired(self):
with self.assertRaises(TimeoutError):
Expand Down
0