I have written a simple HTTP server using asyncio. It works, but I don't always understand how it works,
I have written a simple HTTP server using asyncio. It works, but I don't >always understand how it works, so I was pleased that Python 3.11
introduced some new high-level concepts that hide the gory details. I
want to refactor my code to use these concepts, but I am not finding it
easy.
In simple terms my main loop looked like this -
loop = asyncio.get_event_loop()
server = loop.run_until_complete(
asyncio.start_server(handle_client, host, port))
loop.run_until_complete(setup_companies())
session_check = asyncio.ensure_future(
check_sessions()) # start background task
print('Press Ctrl+C to stop')
try:
loop.run_forever()
except KeyboardInterrupt:
print()
finally:
session_check.cancel() # tell session_check to stop running
loop.run_until_complete(asyncio.wait([session_check]))
server.close()
loop.stop()
Frank Millman wrote at 2023-1-26 12:12 +0200:
I have written a simple HTTP server using asyncio. It works, but I don't
always understand how it works, so I was pleased that Python 3.11
introduced some new high-level concepts that hide the gory details. I
want to refactor my code to use these concepts, but I am not finding it
easy.
In simple terms my main loop looked like this -
   loop = asyncio.get_event_loop()
   server = loop.run_until_complete(
       asyncio.start_server(handle_client, host, port))
   loop.run_until_complete(setup_companies())
   session_check = asyncio.ensure_future(
       check_sessions()) # start background task
   print('Press Ctrl+C to stop')
   try:
       loop.run_forever()
   except KeyboardInterrupt:
       print()
   finally:
       session_check.cancel() # tell session_check to stop running
       loop.run_until_complete(asyncio.wait([session_check]))
       server.close()
       loop.stop()
Why does your code uses several `loop.run*` calls?
In fact, I would define a single coroutine and run that
with `asyncio.run`.
This way, the coroutine can use all `asyncio` features,
including `loop.create_task`.
I have changed it to async, which I call with 'asyncio.run'. It now
looks like this -
   server = await asyncio.start_server(handle_client, host, port)
   await setup_companies()
   session_check = asyncio.create_task(
       check_sessions()) # start background task
   print('Press Ctrl+C to stop')
   try:
       await server.serve_forever()
   except asyncio.CancelledError:
       pass
   finally:
       session_check.cancel() # tell session_check to stop running
       await asyncio.wait([session_check])
       server.close()
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 300 |
Nodes: | 16 (2 / 14) |
Uptime: | 76:10:25 |
Calls: | 6,716 |
Calls today: | 4 |
Files: | 12,247 |
Messages: | 5,357,561 |