asyncio questions
Dieter Maurer
dieter at handshake.de
Thu Jan 26 12:16:01 EST 2023
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`.
More information about the Python-list
mailing list