@@ -812,7 +812,7 @@ def call_at(self, when, callback, *args, context=None):
812812 timer = events .TimerHandle (when , callback , args , self , context )
813813 if timer ._source_traceback :
814814 del timer ._source_traceback [- 1 ]
815- heapq .heappush (self ._scheduled , timer )
815+ heapq .heappush (self ._scheduled , ( when , timer ) )
816816 timer ._scheduled = True
817817 return timer
818818
@@ -1959,28 +1959,29 @@ def _run_once(self):
19591959 # Remove delayed calls that were cancelled if their number
19601960 # is too high
19611961 new_scheduled = []
1962- for handle in self ._scheduled :
1962+ for when_handle in self ._scheduled :
1963+ handle = when_handle [1 ]
19631964 if handle ._cancelled :
19641965 handle ._scheduled = False
19651966 else :
1966- new_scheduled .append (handle )
1967+ new_scheduled .append (when_handle )
19671968
19681969 heapq .heapify (new_scheduled )
19691970 self ._scheduled = new_scheduled
19701971 self ._timer_cancelled_count = 0
19711972 else :
19721973 # Remove delayed calls that were cancelled from head of queue.
1973- while self ._scheduled and self ._scheduled [0 ]._cancelled :
1974+ while self ._scheduled and self ._scheduled [0 ][ 1 ] ._cancelled :
19741975 self ._timer_cancelled_count -= 1
1975- handle = heapq .heappop (self ._scheduled )
1976+ _ , handle = heapq .heappop (self ._scheduled )
19761977 handle ._scheduled = False
19771978
19781979 timeout = None
19791980 if self ._ready or self ._stopping :
19801981 timeout = 0
19811982 elif self ._scheduled :
19821983 # Compute the desired timeout.
1983- timeout = self ._scheduled [0 ]. _when - self .time ()
1984+ timeout = self ._scheduled [0 ][ 0 ] - self .time ()
19841985 if timeout > MAXIMUM_SELECT_TIMEOUT :
19851986 timeout = MAXIMUM_SELECT_TIMEOUT
19861987 elif timeout < 0 :
@@ -1993,23 +1994,24 @@ def _run_once(self):
19931994
19941995 # Handle 'later' callbacks that are ready.
19951996 end_time = self .time () + self ._clock_resolution
1997+ ready = self ._ready
19961998 while self ._scheduled :
1997- handle = self ._scheduled [0 ]
1998- if handle . _when >= end_time :
1999+ when , handle = self ._scheduled [0 ]
2000+ if when >= end_time :
19992001 break
2000- handle = heapq .heappop (self ._scheduled )
2002+ heapq .heappop (self ._scheduled )
20012003 handle ._scheduled = False
2002- self . _ready .append (handle )
2004+ ready .append (handle )
20032005
20042006 # This is the only place where callbacks are actually *called*.
20052007 # All other places just add them to ready.
20062008 # Note: We run all currently scheduled callbacks, but not any
20072009 # callbacks scheduled by callbacks run this time around --
20082010 # they will be run the next time (after another I/O poll).
20092011 # Use an idiom that is thread-safe without using locks.
2010- ntodo = len (self . _ready )
2012+ ntodo = len (ready )
20112013 for i in range (ntodo ):
2012- handle = self . _ready .popleft ()
2014+ handle = ready .popleft ()
20132015 if handle ._cancelled :
20142016 continue
20152017 if self ._debug :
0 commit comments