@@ -347,7 +347,7 @@ def graceful_shutdown(signum, frame):
347
347
self .log .info ("To stop this application, press Control-C" )
348
348
while self .running :
349
349
time .sleep (0.1 )
350
- self . log . info ( "Bye" )
350
+ # exit forever loop and ask platform to end event loop
351
351
self .stop ()
352
352
else :
353
353
raise ValueError ('Unable to register Control-C and KILL signals because this is not the main thread' )
@@ -562,29 +562,31 @@ def stop(self):
562
562
# to allow user application to invoke the "stop" method from a registered service,
563
563
# the system must start a new thread so that the service can finish first.
564
564
#
565
- threading .Thread (target = self ._bye ).start ()
566
-
567
- def _bye (self ):
568
565
if not self .stopped :
566
+ self .log .info ('Bye' )
569
567
# guarantee this stop function to execute only once
570
568
self .stopped = True
571
569
# exit the run_forever loop if any
572
570
self .running = False
571
+ # in case the calling function has just send an event asynchronously
572
+ time .sleep (0.5 )
573
+ threading .Thread (target = self ._bye ).start ()
573
574
574
- def stopping ():
575
- route_list = []
576
- for route in self .get_routes ():
577
- route_list .append (route )
578
- for route in route_list :
579
- self ._remove_route (route )
580
- self ._loop .create_task (full_stop ())
581
-
582
- async def full_stop ():
583
- # give time for registered services to stop
584
- await asyncio .sleep (1.0 )
585
- queue_dir = self .util .normalize_path (self .work_dir + "/queues/" + self .get_origin ())
586
- self .util .cleanup_dir (queue_dir )
587
- self ._loop .stop ()
588
-
589
- self ._cloud .close_connection (1000 , 'bye' , stop_engine = True )
590
- self ._loop .call_soon_threadsafe (stopping )
575
+ def _bye (self ):
576
+ def stopping ():
577
+ route_list = []
578
+ for route in self .get_routes ():
579
+ route_list .append (route )
580
+ for route in route_list :
581
+ self ._remove_route (route )
582
+ self ._loop .create_task (full_stop ())
583
+
584
+ async def full_stop ():
585
+ # give time for registered services to stop
586
+ await asyncio .sleep (1.0 )
587
+ queue_dir = self .util .normalize_path (self .work_dir + "/queues/" + self .get_origin ())
588
+ self .util .cleanup_dir (queue_dir )
589
+ self ._loop .stop ()
590
+
591
+ self ._cloud .close_connection (1000 , 'bye' , stop_engine = True )
592
+ self ._loop .call_soon_threadsafe (stopping )
0 commit comments