line = await reader.readline() if not line: break # Connection was closed writer.write(await process(line)) finally: writer.close() Bruce Merry asyncio: it’s all about the cancellation PyConZA 2017 2 / 5
line = await reader.readline() if not line: break # Connection was closed writer.write(await process(line)) finally: writer.close() loop = asyncio.get_event_loop() task = loop.create_task(do_connection(reader, writer)) ... await task Bruce Merry asyncio: it’s all about the cancellation PyConZA 2017 2 / 5
= await reader.readline() if not line: break # Connection was closed writer.write(await process(line)) finally: writer.close() Bruce Merry asyncio: it’s all about the cancellation PyConZA 2017 3 / 5
= await reader.readline() if not line: break # Connection was closed writer.write(await process(line)) finally: writer.close() task.cancel() Bruce Merry asyncio: it’s all about the cancellation PyConZA 2017 3 / 5
= await reader.readline() if not line: break # Connection was closed writer.write(await process(line)) finally: writer.close() task.cancel() CancelledError Bruce Merry asyncio: it’s all about the cancellation PyConZA 2017 3 / 5
= await reader.readline() if not line: break # Connection was closed writer.write(await process(line)) except asyncio.CancelledError: writer.write(b Server shutting down\n ) raise finally: writer.close() Bruce Merry asyncio: it’s all about the cancellation PyConZA 2017 4 / 5