Skip to content

Commit 6a327d0

Browse files
authored
feat: speed up processing records in the ServiceBrowser (#1143)
1 parent 68871c3 commit 6a327d0

2 files changed

Lines changed: 23 additions & 8 deletions

File tree

src/zeroconf/_services/browser.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import random
2626
import threading
2727
import warnings
28+
from abc import abstractmethod
2829
from collections import OrderedDict
2930
from typing import (
3031
TYPE_CHECKING,
@@ -408,21 +409,14 @@ def async_update_records(self, zc: 'Zeroconf', now: float, records: List[RecordU
408409
for record in records:
409410
self._async_process_record_update(now, record[0], record[1])
410411

412+
@abstractmethod
411413
def async_update_records_complete(self) -> None:
412414
"""Called when a record update has completed for all handlers.
413415
414416
At this point the cache will have the new records.
415417
416418
This method will be run in the event loop.
417419
"""
418-
while self._pending_handlers:
419-
event = self._pending_handlers.popitem(False)
420-
# If there is a queue running (ServiceBrowser)
421-
# get fired in dedicated thread
422-
if self.queue:
423-
self.queue.put(event)
424-
else:
425-
self._fire_service_state_changed_event(event)
426420

427421
def _fire_service_state_changed_event(self, event: Tuple[Tuple[str, str], ServiceStateChange]) -> None:
428422
"""Fire a service state changed event.
@@ -553,3 +547,14 @@ def run(self) -> None:
553547
if event is None:
554548
return
555549
self._fire_service_state_changed_event(event)
550+
551+
def async_update_records_complete(self) -> None:
552+
"""Called when a record update has completed for all handlers.
553+
554+
At this point the cache will have the new records.
555+
556+
This method will be run in the event loop.
557+
"""
558+
assert self.queue is not None
559+
while self._pending_handlers:
560+
self.queue.put(self._pending_handlers.popitem(False))

src/zeroconf/asyncio.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ async def async_cancel(self) -> None:
8282
"""Cancel the browser."""
8383
self._async_cancel()
8484

85+
def async_update_records_complete(self) -> None:
86+
"""Called when a record update has completed for all handlers.
87+
88+
At this point the cache will have the new records.
89+
90+
This method will be run in the event loop.
91+
"""
92+
while self._pending_handlers:
93+
self._fire_service_state_changed_event(self._pending_handlers.popitem(False))
94+
8595

8696
class AsyncZeroconfServiceTypes(ZeroconfServiceTypes):
8797
"""An async version of ZeroconfServiceTypes."""

0 commit comments

Comments
 (0)