Add finalizer

This commit is contained in:
Andrew Svetlov 2015-11-26 20:26:57 +02:00
parent a77702c799
commit aed49a99ab
1 changed files with 12 additions and 5 deletions

View File

@ -30,17 +30,24 @@ def init(loop):
handler = app.make_handler() handler = app.make_handler()
srv = yield from loop.create_server(handler, '127.0.0.1', 8080) srv = yield from loop.create_server(handler, '127.0.0.1', 8080)
print("Server started at http://127.0.0.1:8080") print("Server started at http://127.0.0.1:8080")
return srv, handler return srv, app, handler
@asyncio.coroutine @asyncio.coroutine
def finalize(loop, srv, handler): def finalize(srv, app, handler):
pass sock = srv.sockets[0]
app.loop.remove_reader(sock.fileno())
sock.close()
yield from handler.finish_connections(1.0)
srv.close()
yield from srv.wait_closed()
yield from app.finish()
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
srv, handler = loop.run_until_complete(init(loop)) srv, app, handler = loop.run_until_complete(init(loop))
try: try:
loop.run_forever() loop.run_forever()
except KeyboardInterrupt: except KeyboardInterrupt:
loop.run_until_complete((finalize(loop))) loop.run_until_complete((finalize(srv, app, handler)))