wait the shutdown event instead of running forever

This commit is contained in:
Victor Shyba 2018-12-12 20:13:08 -03:00
parent 1c860e8578
commit 9d772a45d5

View file

@ -111,6 +111,7 @@ class Server:
def stop(self): def stop(self):
for task in reversed(self.cancellable_tasks): for task in reversed(self.cancellable_tasks):
task.cancel() task.cancel()
self.shutdown_event.set()
def run(self): def run(self):
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
@ -118,6 +119,6 @@ class Server:
loop.add_signal_handler(signal.SIGINT, self.stop) loop.add_signal_handler(signal.SIGINT, self.stop)
loop.add_signal_handler(signal.SIGTERM, self.stop) loop.add_signal_handler(signal.SIGTERM, self.stop)
loop.run_until_complete(self.start()) loop.run_until_complete(self.start())
loop.run_forever() loop.run_until_complete(self.shutdown_event.wait())
finally: finally:
loop.run_until_complete(loop.shutdown_asyncgens()) loop.run_until_complete(loop.shutdown_asyncgens())