Skip to content

Commit 87df5af

Browse files
authored
Merge pull request #10 from GhostTypes/perf/optimize-http-client-busy-wait-12909644194619267245
⚡ Optimize busy-wait in HTTP client
2 parents 776e36f + 11f45b1 commit 87df5af

1 file changed

Lines changed: 25 additions & 6 deletions

File tree

flashforge/client.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ def __init__(self, ip_address: str, serial_number: str, check_code: str):
4747

4848
# HTTP client state
4949
self._http_session: Optional[aiohttp.ClientSession] = None
50-
self._http_client_busy = False
50+
self._http_client_event = asyncio.Event()
51+
self._http_client_event.set() # Not busy initially
5152

5253
# TCP client setup
5354
self.tcp_client = TcpClient(ip_address)
@@ -123,17 +124,35 @@ async def initialize(self) -> bool:
123124
print("Failed to connect to printer")
124125
return False
125126

127+
@property
128+
def _http_client_busy(self) -> bool:
129+
"""
130+
Legacy property to maintain internal API compatibility.
131+
Returns True if the client is busy (event is not set), False otherwise.
132+
"""
133+
return not self._http_client_event.is_set()
134+
135+
@_http_client_busy.setter
136+
def _http_client_busy(self, value: bool) -> None:
137+
"""
138+
Sets the busy state using the event.
139+
True means busy (clear event), False means not busy (set event).
140+
"""
141+
if value:
142+
self._http_client_event.clear()
143+
else:
144+
self._http_client_event.set()
145+
126146
async def is_http_client_busy(self) -> bool:
127147
"""
128148
Checks if the HTTP client is currently busy.
149+
Waits until the client is not busy before returning.
129150
130151
Returns:
131-
True if the HTTP client is busy, False otherwise
152+
False (always returns False after waiting, for compatibility)
132153
"""
133-
# Wait a bit if busy to prevent tight loops
134-
while self._http_client_busy:
135-
await asyncio.sleep(0.01)
136-
return self._http_client_busy
154+
await self._http_client_event.wait()
155+
return False
137156

138157
def release_http_client(self) -> None:
139158
"""Releases the HTTP client, allowing it to be used for new requests."""

0 commit comments

Comments
 (0)