@@ -213,7 +213,7 @@ def express_interest(self,
213213 kwargs ['nonce' ] = gen_nonce ()
214214 interest_param = InterestParam .from_dict (kwargs )
215215 interest , final_name = make_interest (name , interest_param , app_param , signer = signer , need_final_name = True )
216- future = aio .get_event_loop ().create_future ()
216+ future = aio .get_running_loop ().create_future ()
217217 node = self ._int_tree .setdefault (final_name , InterestTreeNode ())
218218 node .append_interest (future , interest_param )
219219 self .face .send (interest )
@@ -240,11 +240,13 @@ async def _wait_for_data(self, future: aio.Future, lifetime: int, name: FormalNa
240240 else :
241241 raise ValidationFailure (name , meta_info , content )
242242
243- async def main_loop (self , after_start : Awaitable = None ):
243+ async def main_loop (self , after_start : Awaitable = None ) -> bool :
244244 """
245245 The main loop of NDNApp.
246246
247247 :param after_start: the coroutine to start after connection to NFD is established.
248+ :return: ``True`` if the connection is shutdown not by ``Ctrl+C``.
249+ For example, manually or by the other side.
248250 """
249251 async def starting_task ():
250252 for name , route , validator , need_raw_packet , need_sig_ptrs in self ._autoreg_routes :
@@ -265,12 +267,19 @@ async def starting_task():
265267 elif isinstance (after_start , aio .Task ) or isinstance (after_start , aio .Future ):
266268 after_start .cancel ()
267269 raise
268- task = aio .ensure_future (starting_task ())
270+ task = aio .create_task (starting_task ())
269271 logging .debug ('Connected to NFD node, start running...' )
270- await self .face .run ()
271- self .face .shutdown ()
272+ try :
273+ await self .face .run ()
274+ ret = True
275+ except aio .CancelledError :
276+ logging .info ('Shutting down' )
277+ ret = False
278+ finally :
279+ self .face .shutdown ()
272280 self ._clean_up ()
273281 await task
282+ return ret
274283
275284 def _clean_up (self ):
276285 for node in self ._int_tree .itervalues ():
@@ -285,13 +294,11 @@ def shutdown(self):
285294 logging .info ('Manually shutdown' )
286295 self .face .shutdown ()
287296
288- def run_forever (self , after_start : Awaitable = None ) -> bool :
297+ def run_forever (self , after_start : Awaitable = None ):
289298 """
290299 A non-async wrapper of :meth:`main_loop`.
291300
292301 :param after_start: the coroutine to start after connection to NFD is established.
293- :return: ``True`` if the connection is shutdown not by ``Ctrl+C``.
294- For example, manually or by the other side.
295302
296303 :examples:
297304 .. code-block:: python3
@@ -301,17 +308,10 @@ def run_forever(self, after_start: Awaitable = None) -> bool:
301308 if __name__ == '__main__':
302309 app.run_forever(after_start=main())
303310 """
304- task = self .main_loop (after_start )
305311 try :
306- aio .get_event_loop ().run_until_complete (task )
307- ret = True
312+ aio .run (self .main_loop (after_start ))
308313 except KeyboardInterrupt :
309- logging .info ('Receiving Ctrl+C, shutdown' )
310- ret = False
311- finally :
312- self .face .shutdown ()
313- logging .debug ('Face is down now' )
314- return ret
314+ logging .info ('Receiving Ctrl+C, exit' )
315315
316316 def route (self , name : NonStrictName , validator : Optional [Validator ] = None ,
317317 need_raw_packet : bool = False , need_sig_ptrs : bool = False ):
@@ -355,7 +355,7 @@ def on_interest(name: FormalName, param: InterestParam, app_param):
355355 def decorator (func : Route ):
356356 self ._autoreg_routes .append ((name , func , validator , need_raw_packet , need_sig_ptrs ))
357357 if self .face .running :
358- aio .ensure_future (self .register (name , func , validator , need_raw_packet , need_sig_ptrs ))
358+ aio .create_task (self .register (name , func , validator , need_raw_packet , need_sig_ptrs ))
359359 return func
360360 return decorator
361361
0 commit comments