adafruit / Adafruit_CircuitPython_asyncio

CIrcuitPython subset of CPython asyncio library
MIT License
28 stars 16 forks source link

Use CircuitPython socket support #4

Open dhalbert opened 3 years ago

dhalbert commented 3 years ago

Change asyncio.open_connection() and asynico.start_server() to use CircuitPython's socket support.

thorangutang commented 2 years ago

Yes please. I believe this would allow a non blocking tcp/udp socket to be opened.

furbrain commented 2 years ago

I've tried to fix this with limited success in these functions

furbrain commented 2 years ago

However, the asyncio.StreamReader function does seem to at least start to play well with other "file-like" objects, such as UARTs, the usb_cdc.Serial objects and (not tested yet) the _bleio.CharacteristicBuffer streams - but see my other issue for more info

dhalbert commented 2 years ago
  • CircuitPython uses SocketPool, which needs to be initialised with a radio. We can import wifi and do sp = SocketPool(wifi.radio), but this limits our ability to use other networking options as and when they appear

You could pass in the SocketPool. adafruit_requests and other libraries are agnostic about the source of sockets, and take it as a parameter.

Commod0re commented 4 hours ago

I've taken a few cracks at this and it's really not so simple as that unfortunately. There are a few issues but the biggest thing is that SocketPool sockets do not expose a file descriptor, therefore they can't be used with select (and therefore also IOQueue and therefore also asyncio.stream.Stream etc)

esp-idf's examples around connecting and using non-blocking sockets also depend on having a file descriptor to use with select (see here: https://github.com/espressif/esp-idf/blob/master/examples/protocols/sockets/non_blocking/main/non_blocking_socket_example.c)

other issues, like the strange connection behavior I see with non-blocking sockets on my Qualia S3 with CircuitPython 9.2.1, can be worked around, but this piece is pretty fundamental to this particular problem. SocketPool Sockets' underlying file descriptor need to be exposed via a fileno() method so that select can work with them before Stream will work with it at all

EDITED: asyncio not aiohttp :face_exhaling: