docs: asyncio start_server does not behave as described #13592
Replies: 1 comment
-
Micropython does not support the
In fact all awaitable functions technically return a generator (that is how asyncio is implemented in python). The docs refer to what is returned from I believe the original code will work if you replace |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Micropython version: 1.22.1
Board: W5500-EVB-Pico
Wiznet has a board that adds ethernet capability to a Raspberry Pi Pico - the W5500-EVB-Pico. I'd like to use this to implement network connected instruments in my home lab.
There are plenty of examples on the documentation site, but the TCP examples all seem to use low-level socket programming. The latest Micropython implementations all have asyncio available so why not use the facilities available there?
For example Bytepawn's simple message queue server. Here is a snippet that shows how
start_server
is used:Nope. Doesn't work. The problem seems to be that even though the Micropython documentation says that the asyncio.start_server function works in the same way as the standard library - it doesn't. Specifically, the documentation states that
start_server
returns an instance of theServer
class. It doesn't - it returns a<generator>
.After some digging on the net, I came up with the code below (mainly through the infinite monkey procedure).
This code uses start_server but then runs the resulting generator using an explicitly constructed
asyncio
event_loop
. There is an advantage to this, though, in that this also allows us to start multiple tasks, or multiple servers at the same time.The create_handler function builds a parametrised handler that is passed to start_server. It can be parametrised with a function that transforms from a string to a string. The default is just the identity function.
Run the program and use telnet to connect to to the board on either port 7777 - which acts as an echo server, or port 7778 which uppercases the reply.
Beta Was this translation helpful? Give feedback.
All reactions