← index #5870PR #8895
Related · high · value 4.355
QUERY · ISSUE

extmod/uasyncio: add serve_forever()?

openby belyalovopened 2020-04-06updated 2020-04-07
extmod

Official example from python documentation:

async def main():
    # Get a reference to the event loop as we plan to use
    # low-level APIs.
    loop = asyncio.get_running_loop()

    server = await loop.create_server(
        lambda: EchoServerProtocol(),
        '127.0.0.1', 8888)

    async with server:
        await server.serve_forever()


asyncio.run(main())

Micropython's Server class already has support for async with, but lacks convenient method serve_forever.

Do you think it is make sense to add it?

CANDIDATE · PULL REQUEST

uasyncio/stream.py: Handle cancellation before server start.

mergedby jimmoopened 2022-07-11updated 2024-02-13
extmod

Originally reported here: https://forum.micropython.org/viewtopic.php?f=20&t=12678&p=68845#p68791

The following code:

  server = await asyncio.start_server(...)
  async with server:
    ... code that raises ...

would lose the original exception because the server's task would not have had a chance to be scheduled yet, and so awaiting the task in wait_closed would raise the cancellation instead of the original exception.

In this case the code that raised was calling server.serve_forever() which we don't support.

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied