@@ -1754,6 +1754,22 @@ def generate_ready_queries(self) -> Optional[DNSOutgoing]:
17541754 self ._delay [type_ ] = min (_BROWSER_BACKOFF_LIMIT * 1000 , self ._delay [type_ ] * 2 )
17551755 return out
17561756
1757+ def _seconds_to_wait (self ) -> Optional [float ]:
1758+ """Returns the number of seconds to wait for the next event."""
1759+ # If there are handlers to call
1760+ # we want to process them right away
1761+ if self ._handlers_to_call :
1762+ return None
1763+
1764+ # Wait for the type has the smallest next time
1765+ next_time = min (self ._next_time .values ())
1766+ now = current_time_millis ()
1767+
1768+ if next_time <= now :
1769+ return None
1770+
1771+ return millis_to_seconds (next_time - now )
1772+
17571773
17581774class ServiceBrowser (_ServiceBrowserBase , threading .Thread ):
17591775 """Used to browse for a service of a specific type.
@@ -1785,33 +1801,20 @@ def cancel(self) -> None:
17851801 super ().cancel ()
17861802 self .join ()
17871803
1788- def _wait_for_next_event (self ) -> None :
1789- """Wait for the next handler or time to send queries."""
1790- # If there are handlers to call
1791- # we want to process them right away
1792- if self ._handlers_to_call :
1793- return
1794-
1795- # Wait for the type has the smallest next time
1796- next_time = min (self ._next_time .values ())
1797- now = current_time_millis ()
1798-
1799- if next_time <= now :
1800- return
1801-
1802- with self .zc .condition :
1803- # We must check again while holding the condition
1804- # in case the other thread has added to _handlers_to_call
1805- # between when we checked above when we were not
1806- # holding the condition
1807- if not self ._handlers_to_call :
1808- self .zc .condition .wait (millis_to_seconds (next_time - now ))
1809-
18101804 def run (self ) -> None :
18111805 """Run the browser thread."""
18121806 super ().run ()
18131807 while True :
1814- self ._wait_for_next_event ()
1808+ timeout = self ._seconds_to_wait ()
1809+ if timeout :
1810+ with self .zc .condition :
1811+ # We must check again while holding the condition
1812+ # in case the other thread has added to _handlers_to_call
1813+ # between when we checked above when we were not
1814+ # holding the condition
1815+ if not self ._handlers_to_call :
1816+ self .zc .condition .wait (timeout )
1817+
18151818 if self .zc .done or self .done :
18161819 return
18171820
0 commit comments