Run one full iteration of the event loop. This calls all currently ready callbacks, polls for I/O, schedules the resulting callbacks, and finally schedules 'call_later' callbacks.
(self)
| 1982 | self._timer_cancelled_count += 1 |
| 1983 | |
| 1984 | def _run_once(self): |
| 1985 | """Run one full iteration of the event loop. |
| 1986 | |
| 1987 | This calls all currently ready callbacks, polls for I/O, |
| 1988 | schedules the resulting callbacks, and finally schedules |
| 1989 | 'call_later' callbacks. |
| 1990 | """ |
| 1991 | |
| 1992 | sched_count = len(self._scheduled) |
| 1993 | if (sched_count > _MIN_SCHEDULED_TIMER_HANDLES and |
| 1994 | self._timer_cancelled_count / sched_count > |
| 1995 | _MIN_CANCELLED_TIMER_HANDLES_FRACTION): |
| 1996 | # Remove delayed calls that were cancelled if their number |
| 1997 | # is too high |
| 1998 | new_scheduled = [] |
| 1999 | for handle in self._scheduled: |
| 2000 | if handle._cancelled: |
| 2001 | handle._scheduled = False |
| 2002 | else: |
| 2003 | new_scheduled.append(handle) |
| 2004 | |
| 2005 | heapq.heapify(new_scheduled) |
| 2006 | self._scheduled = new_scheduled |
| 2007 | self._timer_cancelled_count = 0 |
| 2008 | else: |
| 2009 | # Remove delayed calls that were cancelled from head of queue. |
| 2010 | while self._scheduled and self._scheduled[0]._cancelled: |
| 2011 | self._timer_cancelled_count -= 1 |
| 2012 | handle = heapq.heappop(self._scheduled) |
| 2013 | handle._scheduled = False |
| 2014 | |
| 2015 | timeout = None |
| 2016 | if self._ready or self._stopping: |
| 2017 | timeout = 0 |
| 2018 | elif self._scheduled: |
| 2019 | # Compute the desired timeout. |
| 2020 | timeout = self._scheduled[0]._when - self.time() |
| 2021 | if timeout > MAXIMUM_SELECT_TIMEOUT: |
| 2022 | timeout = MAXIMUM_SELECT_TIMEOUT |
| 2023 | elif timeout < 0: |
| 2024 | timeout = 0 |
| 2025 | |
| 2026 | event_list = self._selector.select(timeout) |
| 2027 | self._process_events(event_list) |
| 2028 | # Needed to break cycles when an exception occurs. |
| 2029 | event_list = None |
| 2030 | |
| 2031 | # Handle 'later' callbacks that are ready. |
| 2032 | now = self.time() |
| 2033 | # Ensure that `end_time` is strictly increasing |
| 2034 | # when the clock resolution is too small. |
| 2035 | end_time = now + max(self._clock_resolution, math.ulp(now)) |
| 2036 | while self._scheduled: |
| 2037 | handle = self._scheduled[0] |
| 2038 | if handle._when >= end_time: |
| 2039 | break |
| 2040 | handle = heapq.heappop(self._scheduled) |
| 2041 | handle._scheduled = False |
no test coverage detected