Skip to content

Commit 604a2d0

Browse files
committed
Clean up asyncio usage
1 parent 17ae1be commit 604a2d0

File tree

2 files changed

+16
-24
lines changed

2 files changed

+16
-24
lines changed

src/viser/infra/_async_message_buffer.py

+13-23
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
1+
from __future__ import annotations
2+
13
import asyncio
24
import dataclasses
35
import time
46
from asyncio.events import AbstractEventLoop
5-
from typing import (
6-
Any,
7-
AsyncGenerator,
8-
Awaitable,
9-
Dict,
10-
Optional,
11-
Sequence,
12-
Set,
13-
Union,
14-
cast,
15-
)
7+
from typing import Any, AsyncGenerator, Dict, Optional, Sequence, Set, Union, cast
168

179
from typing_extensions import Literal, TypeGuard
1810

@@ -70,11 +62,11 @@ async def window_generator(
7062
# Wait until there are new messages available.
7163
most_recent_message_id = self.message_counter - 1
7264
while last_sent_id >= most_recent_message_id:
73-
next_message = self.message_event.wait()
65+
next_message = asyncio.create_task(self.message_event.wait())
66+
flush_wait = asyncio.create_task(self._flush_event.wait())
7467
send_window = False
7568
try:
76-
flush_wait = self._flush_event.wait()
77-
done, pending = await asyncio.wait( # type: ignore
69+
done, pending = await asyncio.wait(
7870
[flush_wait, next_message],
7971
timeout=window.max_time_until_ready(),
8072
return_when=asyncio.FIRST_COMPLETED,
@@ -147,28 +139,26 @@ def append_to_window(self, message: Union[Message, DoneSentinel]) -> None:
147139

148140
async def wait_and_append_to_window(
149141
self,
150-
message: Awaitable[Union[Message, DoneSentinel]],
142+
message_task: asyncio.Task[Union[Message, DoneSentinel]],
151143
flush_event: asyncio.Event,
152144
) -> bool:
153145
"""Async version of `append_to_window()`. Returns `True` if successful, `False`
154146
if timed out."""
155147
if len(self._window) == 0:
156-
self.append_to_window(await message)
148+
self.append_to_window(await message_task)
157149
return True
158150

159-
message = asyncio.shield(message)
160-
flush_wait = asyncio.shield(flush_event.wait())
161-
(done, pending) = await asyncio.wait( # type: ignore
162-
[message, flush_wait],
151+
flush_wait = asyncio.create_task(flush_event.wait())
152+
done, pending = await asyncio.wait(
153+
[message_task, flush_wait],
163154
timeout=self.max_time_until_ready(),
164155
return_when=asyncio.FIRST_COMPLETED,
165156
)
166157
del pending
167158
if flush_wait in done:
168159
flush_event.clear()
169-
flush_wait.cancel()
170-
if message in cast(Set[Any], done): # Cast to prevent type narrowing.
171-
self.append_to_window(await message)
160+
if message_task in cast(Set[Any], done): # Cast to prevent type narrowing.
161+
self.append_to_window(await message_task)
172162
return True
173163
return False
174164

src/viser/infra/_infra.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,9 @@ def handle_incoming(message: Message) -> None:
286286
# queue get() tasks, which suppresses a "Task was destroyed but it is
287287
# pending" error.
288288
await client_state.message_buffer.put(DONE_SENTINEL)
289-
self._flush_event_from_client_id.pop(client_id)
289+
290+
# Trigger then delete the flush event.
291+
self._flush_event_from_client_id.pop(client_id).set()
290292

291293
# Disconnection callbacks.
292294
for cb in self._client_disconnect_cb:

0 commit comments

Comments
 (0)