|
1 | | -import threading |
2 | 1 | import time |
3 | 2 | from contextlib import asynccontextmanager |
4 | 3 | from dataclasses import dataclass, field |
5 | 4 | from typing import Optional |
6 | 5 |
|
7 | | -from anyio import sleep |
| 6 | +from anyio import Event, fail_after, sleep |
8 | 7 | from anyio._backends._asyncio import StreamReaderWrapper, StreamWriterWrapper |
9 | 8 | from serial_asyncio import open_serial_connection |
10 | 9 |
|
@@ -50,7 +49,7 @@ class NVDemuxSerial(Driver): |
50 | 49 | poll_interval: float = field(default=1.0) |
51 | 50 |
|
52 | 51 | # Internal state (not init params) |
53 | | - _ready: threading.Event = field(init=False, default_factory=threading.Event) |
| 52 | + _ready: Event = field(init=False, default_factory=Event) |
54 | 53 | _registered: bool = field(init=False, default=False) |
55 | 54 |
|
56 | 55 | def __post_init__(self): |
@@ -106,16 +105,14 @@ async def connect(self): |
106 | 105 | Waits for the demuxer to be ready (device connected and pts path discovered) |
107 | 106 | before opening the serial connection. |
108 | 107 | """ |
109 | | - # Wait for ready state |
110 | | - start_time = time.monotonic() |
111 | | - while not self._ready.is_set(): |
112 | | - elapsed = time.monotonic() - start_time |
113 | | - if elapsed >= self.timeout: |
114 | | - raise TimeoutError( |
115 | | - f"Timeout waiting for demuxer to become ready (device pattern: {self.device})" |
116 | | - ) |
117 | | - # Use a short sleep to allow checking ready state |
118 | | - await sleep(0.1) |
| 108 | + # Wait for ready state with timeout |
| 109 | + try: |
| 110 | + with fail_after(self.timeout): |
| 111 | + await self._ready.wait() |
| 112 | + except TimeoutError: |
| 113 | + raise TimeoutError( |
| 114 | + f"Timeout waiting for demuxer to become ready (device pattern: {self.device})" |
| 115 | + ) from None |
119 | 116 |
|
120 | 117 | # Get the current pts path from manager (retry until timeout) |
121 | 118 | manager = DemuxerManager.get_instance() |
|
0 commit comments