Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: speed up processing records in the ServiceBrowser #1143

Merged
merged 1 commit into from
Apr 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 13 additions & 8 deletions src/zeroconf/_services/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import random
import threading
import warnings
from abc import abstractmethod
from collections import OrderedDict
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -400,21 +401,14 @@ def async_update_records(self, zc: 'Zeroconf', now: float, records: List[RecordU
for record in records:
self._async_process_record_update(now, record[0], record[1])

@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.
"""
while self._pending_handlers:
event = self._pending_handlers.popitem(False)
# If there is a queue running (ServiceBrowser)
# get fired in dedicated thread
if self.queue:
self.queue.put(event)
else:
self._fire_service_state_changed_event(event)

def _fire_service_state_changed_event(self, event: Tuple[Tuple[str, str], ServiceStateChange]) -> None:
"""Fire a service state changed event.
Expand Down Expand Up @@ -545,3 +539,14 @@ def run(self) -> None:
if event is None:
return
self._fire_service_state_changed_event(event)

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.
"""
assert self.queue is not None
while self._pending_handlers:
self.queue.put(self._pending_handlers.popitem(False))
10 changes: 10 additions & 0 deletions src/zeroconf/asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ 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.
"""
while self._pending_handlers:
self._fire_service_state_changed_event(self._pending_handlers.popitem(False))


class AsyncZeroconfServiceTypes(ZeroconfServiceTypes):
"""An async version of ZeroconfServiceTypes."""
Expand Down