extmod/uasyncio: add serve_forever()?
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?
extmod/uasyncio: Allow catching exceptions in start_server.
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.