← index #5870PR #7444
Related · high · value 2.332
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

extmod/uasyncio: Allow catching exceptions in start_server.

closedby andrewleechopened 2021-06-22updated 2021-06-25
extmod

Currently when using uasyncio.start_server() the socket configuration is done inside a uasyncio.create_task() background function.

If the address:port is already in use however this throws a:

Traceback (most recent call last):
  File "uasyncio/stream.py", line 1, in start_server
  File "uasyncio/stream.py", line 1, in _serve
OSError: [Errno 98] EADDRINUSE

which cannot be cleanly caught behind the create_task().

This PR splits up the server port init so that the initial socket configuration is in a normal awaited function, before deferring the listen while loop to the background uasyncio.create_task() function. This means that any OSError from the initial socket configuration is propagated directly up the call stack.

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