Updated server example with context manager to close the server socket automatically

This commit is contained in:
nocturn9x 2021-08-26 16:54:13 +02:00
parent 9bb091cdef
commit e4a4c45f65
1 changed files with 5 additions and 4 deletions

View File

@ -19,10 +19,11 @@ async def serve(bind_address: tuple):
await sock.listen(5)
logging.info(f"Serving asynchronously at {bind_address[0]}:{bind_address[1]}")
async with giambio.create_pool() as pool:
while True:
conn, address_tuple = await sock.accept()
logging.info(f"{address_tuple[0]}:{address_tuple[1]} connected")
await pool.spawn(handler, conn, address_tuple)
async with sock:
while True:
conn, address_tuple = await sock.accept()
logging.info(f"{address_tuple[0]}:{address_tuple[1]} connected")
await pool.spawn(handler, conn, address_tuple)
async def handler(sock: AsyncSocket, client_address: tuple):