Skip to content

Commit

Permalink
feat: improve AsyncServiceBrowser performance (#1273)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed Sep 14, 2023
1 parent 2734db9 commit 0c88ecf
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/zeroconf/_services/browser.pxd
Expand Up @@ -74,3 +74,5 @@ cdef class _ServiceBrowserBase(RecordUpdateListener):
cpdef _async_send_ready_queries(self, object now)

cpdef _cancel_send_timer(self)

cpdef async_update_records_complete(self)
10 changes: 7 additions & 3 deletions src/zeroconf/_services/browser.py
Expand Up @@ -25,7 +25,6 @@
import random
import threading
import warnings
from abc import abstractmethod
from types import TracebackType # noqa # used in type hints
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -437,14 +436,18 @@ def async_update_records(self, zc: 'Zeroconf', now: float_, records: List[Record
for type_, name in self._names_matching_types((record.name,)):
self._enqueue_callback(SERVICE_STATE_CHANGE_UPDATED, type_, name)

@abstractmethod
def async_update_records_complete(self) -> None:
"""Called when a record update has completed for all handlers.
At this point the cache will have the new records.
This method will be run in the event loop.
This method is expected to be overridden by subclasses.
"""
for pending in self._pending_handlers.items():
self._fire_service_state_changed_event(pending)
self._pending_handlers.clear()

def _fire_service_state_changed_event(self, event: Tuple[Tuple[str, str], ServiceStateChange]) -> None:
"""Fire a service state changed event.
Expand All @@ -454,7 +457,8 @@ def _fire_service_state_changed_event(self, event: Tuple[Tuple[str, str], Servic
When running with AsyncServiceBrowser, this will happen in the event loop.
"""
name_type, state_change = event
name_type = event[0]
state_change = event[1]
self._service_state_changed.fire(
zeroconf=self.zc,
service_type=name_type[1],
Expand Down
11 changes: 0 additions & 11 deletions src/zeroconf/asyncio.py
Expand Up @@ -82,17 +82,6 @@ async def async_cancel(self) -> None:
"""Cancel the browser."""
self._async_cancel()

def async_update_records_complete(self) -> None:
"""Called when a record update has completed for all handlers.
At this point the cache will have the new records.
This method will be run in the event loop.
"""
for pending in self._pending_handlers.items():
self._fire_service_state_changed_event(pending)
self._pending_handlers.clear()

async def __aenter__(self) -> 'AsyncServiceBrowser':
return self

Expand Down

0 comments on commit 0c88ecf

Please sign in to comment.