Skip to content

Commit b4319a7

Browse files
committed
Replace wait_event_or_timeout internals with async_timeout
Its unlikely that https://bugs.python.org/issue39032 and python/cpython#83213 will be fixed soon. While we moved away from an asyncio.Condition, we still has a similar problem with waiting for an asyncio.Event which wait_event_or_timeout played well with. async_timeout avoids creating a task so its a bit more efficient. Since we call these when resolving ServiceInfo, avoiding task creation will resolve a performance problem when ServiceBrowsers startup as they tend to create task storms when coupled with ServiceInfo lookups.
1 parent 88323d0 commit b4319a7

3 files changed

Lines changed: 7 additions & 22 deletions

File tree

‎requirements-dev.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
async_timeout>=4.0.1
12
autopep8
23
black;implementation_name=="cpython"
34
bump2version

‎setup.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,5 @@
4545
'Programming Language :: Python :: Implementation :: PyPy',
4646
],
4747
keywords=['Bonjour', 'Avahi', 'Zeroconf', 'Multicast DNS', 'Service Discovery', 'mDNS'],
48-
install_requires=['ifaddr>=0.1.7'],
48+
install_requires=['async_timeout>=4.0.1', 'ifaddr>=0.1.7'],
4949
)

‎zeroconf/_utils/asyncio.py‎

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import contextlib
2626
from typing import Any, Awaitable, Coroutine, Optional, Set
2727

28+
import async_timeout
29+
2830
from .time import millis_to_seconds
2931
from .._exceptions import EventLoopBlocked
3032
from ..const import _LOADED_SYSTEM_TIMEOUT
@@ -35,28 +37,11 @@
3537
_WAIT_FOR_LOOP_TASKS_TIMEOUT = 3 # Must be larger than _TASK_AWAIT_TIMEOUT
3638

3739

38-
# Switch to asyncio.wait_for once https://bugs.python.org/issue39032 is fixed
3940
async def wait_event_or_timeout(event: asyncio.Event, timeout: float) -> None:
4041
"""Wait for an event or timeout."""
41-
loop = asyncio.get_event_loop()
42-
future = loop.create_future()
43-
44-
def _handle_timeout_or_wait_complete(*_: Any) -> None:
45-
if not future.done():
46-
future.set_result(None)
47-
48-
timer_handle = loop.call_later(timeout, _handle_timeout_or_wait_complete)
49-
event_wait = loop.create_task(event.wait())
50-
event_wait.add_done_callback(_handle_timeout_or_wait_complete)
51-
52-
try:
53-
await future
54-
finally:
55-
timer_handle.cancel()
56-
if not event_wait.done():
57-
event_wait.cancel()
58-
with contextlib.suppress(asyncio.CancelledError):
59-
await event_wait
42+
with contextlib.suppress(asyncio.TimeoutError):
43+
async with async_timeout.timeout(timeout):
44+
await event.wait()
6045

6146

6247
async def _async_get_all_tasks(loop: asyncio.AbstractEventLoop) -> Set[asyncio.Task]:
@@ -114,7 +99,6 @@ def shutdown_loop(loop: asyncio.AbstractEventLoop) -> None:
11499
loop.call_soon_threadsafe(loop.stop)
115100

116101

117-
# Remove the call to _get_running_loop once we drop python 3.6 support
118102
def get_running_loop() -> Optional[asyncio.AbstractEventLoop]:
119103
"""Check if an event loop is already running."""
120104
with contextlib.suppress(RuntimeError):

0 commit comments

Comments
 (0)