@@ -613,94 +613,93 @@ def listen(self, port_or_options, handler=None):
613
613
self .SERVER_HOST = (
614
614
"0.0.0.0" if isinstance (port_or_options , int ) else port_or_options .host
615
615
)
616
- self .server .listen (port_or_options , handler )
617
- return self
618
-
619
- def run (self ):
620
616
if not self .lifespan :
621
- self .server .run ()
617
+ self .server .listen ( port_or_options , handler )
622
618
return self
623
-
619
+
624
620
scope = {"type" : "lifespan" , "asgi" : {"version" : "3.0" , "spec_version" : "2.3" }}
625
621
626
- lifespan_loop = Loop (lambda loop , error , response : logging .error ("Uncaught Exception: %s" % str (error )))
627
- is_starting = True
628
- is_stopped = False
629
- status = 0 # 0 starting, 1 ok, 2 error, 3 stoping, 4 stopped, 5 stopped with error, 6 no lifespan
630
- status_message = ""
631
- stop_future = lifespan_loop .create_future ()
622
+ asgi_app = self
623
+ self .is_starting = True
624
+ self .is_stopped = False
625
+ self .status = 0 # 0 starting, 1 ok, 2 error, 3 stoping, 4 stopped, 5 stopped with error, 6 no lifespan
626
+ self .status_message = ""
627
+ self .stop_future = self .server .loop .create_future ()
628
+
632
629
async def send (options ):
633
- nonlocal status , status_message , is_stopped
630
+ nonlocal asgi_app
634
631
type = options ["type" ]
635
- status_message = options .get ("message" , "" )
632
+ asgi_app . status_message = options .get ("message" , "" )
636
633
if type == "lifespan.startup.complete" :
637
- status = 1
634
+ asgi_app .status = 1
635
+ asgi_app .server .listen (port_or_options , handler )
638
636
elif type == "lifespan.startup.failed" :
639
- is_stopped = True
640
- status = 2
637
+ asgi_app . is_stopped = True
638
+ asgi_app . status = 2
641
639
elif type == "lifespan.shutdown.complete" :
642
- is_stopped = True
643
- status = 4
640
+ asgi_app . is_stopped = True
641
+ asgi_app . status = 4
644
642
elif type == "lifespan.shutdown.failed" :
645
- is_stopped = True
646
- status = 5
643
+ asgi_app . is_stopped = True
644
+ asgi_app . status = 5
647
645
648
646
async def receive ():
649
- nonlocal is_starting , is_stopped
650
- while not is_stopped :
651
- if is_starting :
652
- is_starting = False
647
+ nonlocal asgi_app
648
+ while not asgi_app . is_stopped :
649
+ if asgi_app . is_starting :
650
+ asgi_app . is_starting = False
653
651
return {
654
652
"type" : "lifespan.startup" ,
655
653
"asgi" : {"version" : "3.0" , "spec_version" : "2.3" },
656
654
}
657
- return await stop_future
655
+ return await asgi_app . stop_future
658
656
659
657
async def task_wrapper (task ):
660
- nonlocal status
658
+ nonlocal asgi_app
661
659
try :
662
660
return await task
663
661
except Exception as error :
664
662
try :
665
663
# just log in console the error to call attention
666
664
logging .error ("Uncaught Exception: %s" % str (error ))
667
- status = 6 # no more lifespan
665
+ if asgi_app .status < 2 :
666
+ asgi_app .status = 6 # no more lifespan
667
+ asgi_app .server .listen (port_or_options , handler )
668
668
finally :
669
669
return None
670
670
671
671
# start lifespan
672
- lifespan_loop .ensure_future (task_wrapper (self .app (scope , receive , send )))
673
-
674
- # run until start or fail
675
- while status == 0 :
676
- lifespan_loop .run_once ()
677
-
672
+ self .server .loop .ensure_future (task_wrapper (self .app (scope , receive , send )))
673
+ self .server .run ()
678
674
# failed to start
679
- if status == 2 :
680
- logging .error ("Startup failed: %s" % str (status_message ))
675
+ if self .status == 2 :
676
+ logging .error ("Startup failed: %s" % str (self .status_message ))
677
+ return self
678
+ return self
679
+
680
+ def run (self ):
681
+ if not self .lifespan :
682
+ self .server .run ()
681
683
return self
682
684
683
685
# run app
684
686
self .server .run ()
685
-
686
687
# no more lifespan events
687
- if status == 6 :
688
+ if self . status == 6 :
688
689
return self
689
-
690
690
# signal stop
691
- status = 3
692
- stop_future .set_result ({
691
+ self . status = 3
692
+ self . stop_future .set_result ({
693
693
"type" : "lifespan.shutdown" ,
694
694
"asgi" : {"version" : "3.0" , "spec_version" : "2.3" },
695
695
})
696
-
697
696
# run until end or fail
698
- while status == 3 :
699
- lifespan_loop .run_once ()
697
+ while self . status == 3 :
698
+ self . server . loop .run_once ()
700
699
701
700
# failed to stop
702
- if status == 5 :
703
- logging .error ("Shutdown failed: %s" % str (status_message ))
701
+ if self . status == 5 :
702
+ logging .error ("Shutdown failed: %s" % str (self . status_message ))
704
703
return self
705
704
706
705
def __del__ (self ):
0 commit comments