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)
| 1964 | self._timer_cancelled_count += 1 |
| 1965 | |
| 1966 | def _run_once(self): |
| 1967 | """Run one full iteration of the event loop. |
| 1968 | |
| 1969 | This calls all currently ready callbacks, polls for I/O, |
| 1970 | schedules the resulting callbacks, and finally schedules |
| 1971 | 'call_later' callbacks. |
| 1972 | """ |
| 1973 | |
| 1974 | sched_count = len(self._scheduled) |
| 1975 | if (sched_count > _MIN_SCHEDULED_TIMER_HANDLES and |
| 1976 | self._timer_cancelled_count / sched_count > |
| 1977 | _MIN_CANCELLED_TIMER_HANDLES_FRACTION): |
| 1978 | # Remove delayed calls that were cancelled if their number |
| 1979 | # is too high |
| 1980 | new_scheduled = [] |
| 1981 | for handle in self._scheduled: |
| 1982 | if handle._cancelled: |
| 1983 | handle._scheduled = False |
| 1984 | else: |
| 1985 | new_scheduled.append(handle) |
| 1986 | |
| 1987 | heapq.heapify(new_scheduled) |
| 1988 | self._scheduled = new_scheduled |
| 1989 | self._timer_cancelled_count = 0 |
| 1990 | else: |
| 1991 | # Remove delayed calls that were cancelled from head of queue. |
| 1992 | while self._scheduled and self._scheduled[0]._cancelled: |
| 1993 | self._timer_cancelled_count -= 1 |
| 1994 | handle = heapq.heappop(self._scheduled) |
| 1995 | handle._scheduled = False |
| 1996 | |
| 1997 | timeout = None |
| 1998 | if self._ready or self._stopping: |
| 1999 | timeout = 0 |
| 2000 | elif self._scheduled: |
| 2001 | # Compute the desired timeout. |
| 2002 | timeout = self._scheduled[0]._when - self.time() |
| 2003 | if timeout > MAXIMUM_SELECT_TIMEOUT: |
| 2004 | timeout = MAXIMUM_SELECT_TIMEOUT |
| 2005 | elif timeout < 0: |
| 2006 | timeout = 0 |
| 2007 | |
| 2008 | event_list = self._selector.select(timeout) |
| 2009 | self._process_events(event_list) |
| 2010 | # Needed to break cycles when an exception occurs. |
| 2011 | event_list = None |
| 2012 | |
| 2013 | # Handle 'later' callbacks that are ready. |
| 2014 | end_time = self.time() + self._clock_resolution |
| 2015 | while self._scheduled: |
| 2016 | handle = self._scheduled[0] |
| 2017 | if handle._when >= end_time: |
| 2018 | break |
| 2019 | handle = heapq.heappop(self._scheduled) |
| 2020 | handle._scheduled = False |
| 2021 | self._ready.append(handle) |
| 2022 | |
| 2023 | # This is the only place where callbacks are actually *called*. |
no test coverage detected