@@ -70,10 +70,6 @@ def _format_pipe(fd):
7070 return repr (fd )
7171
7272
73- class _StopError (BaseException ):
74- """Raised to stop the event loop."""
75-
76-
7773def _check_resolved_address (sock , address ):
7874 # Ensure that the address is already resolved to avoid the trap of hanging
7975 # the entire event loop when the address requires doing a DNS lookup.
@@ -118,9 +114,6 @@ def _check_resolved_address(sock, address):
118114 "got host %r: %s"
119115 % (host , err ))
120116
121- def _raise_stop_error (* args ):
122- raise _StopError
123-
124117
125118def _run_until_complete_cb (fut ):
126119 exc = fut ._exception
@@ -129,7 +122,7 @@ def _run_until_complete_cb(fut):
129122 # Issue #22429: run_forever() already finished, no need to
130123 # stop it.
131124 return
132- _raise_stop_error ()
125+ fut . _loop . stop ()
133126
134127
135128class Server (events .AbstractServer ):
@@ -184,6 +177,7 @@ class BaseEventLoop(events.AbstractEventLoop):
184177 def __init__ (self ):
185178 self ._timer_cancelled_count = 0
186179 self ._closed = False
180+ self ._stopping = False
187181 self ._ready = collections .deque ()
188182 self ._scheduled = []
189183 self ._default_executor = None
@@ -298,11 +292,11 @@ def run_forever(self):
298292 self ._thread_id = threading .get_ident ()
299293 try :
300294 while True :
301- try :
302- self ._run_once ()
303- except _StopError :
295+ self ._run_once ()
296+ if self ._stopping :
304297 break
305298 finally :
299+ self ._stopping = False
306300 self ._thread_id = None
307301 self ._set_coroutine_wrapper (False )
308302
@@ -345,11 +339,10 @@ def run_until_complete(self, future):
345339 def stop (self ):
346340 """Stop running the event loop.
347341
348- Every callback scheduled before stop() is called will run. Callbacks
349- scheduled after stop() is called will not run. However, those callbacks
350- will run if run_forever is called again later.
342+ Every callback already scheduled will still run. This simply informs
343+ run_forever to stop looping after a complete iteration.
351344 """
352- self .call_soon ( _raise_stop_error )
345+ self ._stopping = True
353346
354347 def close (self ):
355348 """Close the event loop.
@@ -1194,7 +1187,7 @@ def _run_once(self):
11941187 handle ._scheduled = False
11951188
11961189 timeout = None
1197- if self ._ready :
1190+ if self ._ready or self . _stopping :
11981191 timeout = 0
11991192 elif self ._scheduled :
12001193 # Compute the desired timeout.
0 commit comments