adafruit / Adafruit_CircuitPython_asyncio

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

asyncio.start_server relies on usocket, which is not available for circuitpython #62

Closed robogame-dev closed 6 months ago

robogame-dev commented 11 months ago

asyncio.start_server does not start because there is no usocket.

If anyone has a workaround for running a TCP server without polling or blocking please lmk - in the meantime it's possible to poll a non-blocking TCP socket with socketpool, but obviously without the benefits of asyncio for performance.

Minimal reproduction code - be sure to update with your specific WiFi info below:

import asyncio
import wifi

wificreds = ["your_ssidname", "your_password"]

while wifi.radio.ipv4_address is None:
    found_networks = wifi.radio.start_scanning_networks()
    for network in found_networks:
        if network.ssid == wificreds[0]:
            wifi.radio.stop_scanning_networks()
            wifi.radio.connect(*wificreds)
    if wifi.radio.ipv4_address is not None:
        break
print("connected to wifi")

async def run_server():
    await asyncio.start_server(connection_callback, wifi.radio.ipv4_address, 8747)

def connection_callback(reader: asyncio.StreamReader, writer: asyncio.StreamWriter):
    print("incoming connection")

asyncio.run(run_server())

The output of running that code on the latest CircuitPython 8:

code.py output: connected to wifi Traceback (most recent call last): File "code.py", line 26, in File "asyncio/core.py", line 312, in run File "asyncio/core.py", line 271, in run_until_complete File "asyncio/core.py", line 256, in run_until_complete File "code.py", line 19, in run_server File "asyncio/stream.py", line 231, in start_server ImportError: no module named 'usocket'

Code done running.

imnotjames commented 11 months ago

I think this is a duplicate of #35