@@ -123,17 +123,7 @@ def _traits(cls) -> List[str]:
123123
124124 @classmethod
125125 def main (cls ):
126- """Run the event loop."""
127- loop = asyncio .get_event_loop ()
128- if sys .platform .startswith ("win" ):
129- signals = ()
130- else :
131- signals = (signal .SIGHUP , signal .SIGTERM , signal .SIGINT )
132- for s in signals :
133- loop .add_signal_handler (
134- s , lambda s = s : asyncio .create_task (cls .shutdown_all (s , loop ))
135- )
136-
126+ """parse arguments and start the event loop"""
137127 parser = argparse .ArgumentParser ()
138128 parser .add_argument (
139129 "--config" ,
@@ -199,19 +189,26 @@ def main(cls):
199189 with open (config_filepath , "rb" ) as f :
200190 config_file = tomli .load (f )
201191
202- loop . create_task ( cls . _main ( config_filepath , config_file , args ))
192+ # Run the event loop
203193 try :
204- loop . run_forever ( )
194+ asyncio . run ( cls . _main ( config_filepath , config_file , args ) )
205195 except asyncio .CancelledError :
206196 pass
207- finally :
208- loop .close ()
209197
210198 @classmethod
211199 async def _main (cls , config_filepath , config_file , args = None ):
212- """Parse command line arguments, start event loop tasks ."""
200+ """Parse command line arguments, run event loop."""
213201 loop = asyncio .get_running_loop ()
214- cls .__servers = []
202+ if sys .platform .startswith ("win" ):
203+ signals = ()
204+ else :
205+ signals = (signal .SIGHUP , signal .SIGTERM , signal .SIGINT )
206+ for s in signals :
207+ loop .add_signal_handler (
208+ s , lambda s = s : asyncio .create_task (cls .shutdown_all (s , loop ))
209+ )
210+
211+ cls .__servers = set ()
215212 for section in config_file :
216213 if section == "shared-settings" :
217214 continue
@@ -225,7 +222,7 @@ async def _main(cls, config_filepath, config_file, args=None):
225222
226223 while cls .__servers :
227224 awaiting = cls .__servers
228- cls .__servers = []
225+ cls .__servers = set ()
229226 await asyncio .wait (awaiting )
230227 await asyncio .sleep (1 )
231228 loop .stop ()
@@ -252,7 +249,9 @@ def server(daemon):
252249 server (daemon ), config .get ("host" , "" ), config .get ("port" , None )
253250 )
254251 daemon ._server = ser
255- cls .__servers .append (asyncio .create_task (ser .serve_forever ()))
252+ task = asyncio .create_task (ser .serve_forever ())
253+ cls .__servers .add (task )
254+ task .add_done_callback (cls .__servers .discard )
256255
257256 @classmethod
258257 def _parse_config (cls , config_file , section , args = None ):
@@ -297,16 +296,7 @@ async def shutdown_all(cls, sig, loop):
297296 # This is done after cancelling so that shutdown tasks which require the loop
298297 # are not themselves cancelled.
299298 [d .close () for d in cls ._daemons ]
300- tasks = [
301- t
302- for t in asyncio .all_tasks ()
303- if (
304- t is not asyncio .current_task ()
305- and "serve_forever" not in t .get_coro ().__repr__ ()
306- )
307- ]
308- for task in tasks :
309- logger .info (task .get_coro ())
299+ tasks = [t for t in asyncio .all_tasks () if t is not asyncio .current_task ()]
310300 await asyncio .gather (* tasks , return_exceptions = True )
311301 [d ._save_state () for d in cls ._daemons ]
312302 if hasattr (signal , "SIGHUP" ) and sig == signal .SIGHUP :
0 commit comments