Presto IRQ

Also strange: your clients seem to open a connection (the accept()call returns) and then don’t send any data for one minute?!

I have to be more specific on the timeout: you can set a timeout on the server-socket (s in your code), and on the socket that is returned by the accept()-call (cl in your code). The timeout on the server socket will give you fast-fail behavior on accept(). the timeout on cl will close the connection if no data arrives.

if you feel the need then you could have a look at the micropython async sockets code and their examples.

To end my bombardment prattling on about the async stuff I give you a final snippet (with some sympathies, though I hope it may help:) . Actually I’ve been meaning to give the microdot async web server a go, so I installed it on one pico, and created an async client on another so I give these 2 snippets as examples. Of course you would need to install the microdot and the aiohttp modules for the snippets to work.

The sever program

#  default microdot port is 5000
#  view in browser with - e.g. http://10.0.1.66:5000

import asyncio
from microdot import Microdot

# do your own code for the wifi connection
import wifi_connect as wifi
status = wifi.wlan_connect()
print('network status: ', status)


# create a simple server
app = Microdot()

@app.route('/')
async def index(request):
    return 'Hello, world!'

async def main():
    # start the server in a background task
    server = asyncio.create_task(app.start_server())

    # ... do other asynchronous work on the server pico here ...

    # cleanup before ending the application
    await server

try:
    asyncio.run(main())
except KeyboardInterrupt:
    print('Ctl C')
finally:
    print('finally is being actioned')
    asyncio.new_event_loop()    



And the client program:

 
import aiohttp
import asyncio

# do your own code for the wifi connection
import wifi_connect as wifi
status = wifi.wlan_connect()
print('network status: ', status)


# the URL of my pico server
URL = "http://10.0.1.66:5000"
print(URL)

async def fetch(client):
    async with client.get(URL) as resp:
        assert resp.status == 200
        return await resp.text()

async def do_presto_things():
    count = 1
    while True:
        print('working away - count =',count)
        count +=1
        await asyncio.sleep(2)

async def do_socket_stuff():
    while True:
        async with aiohttp.ClientSession() as client:
            html = await fetch(client)
            print(html)
        await asyncio.sleep(1)

async def main():
    asyncio.create_task(do_presto_things())
    asyncio.create_task(do_socket_stuff())
    
    while True:
        await asyncio.sleep(0)               

try:
    asyncio.run(main())
except KeyboardInterrupt:
    print('Ctl C')
finally:
    print('finally is being actioned')
    asyncio.new_event_loop()        
        


Ok, a bit of fun and some more snippets to tuck away in my examples file. But I hope it gives a flavour of the async coding that allows full concurrent programming and as @bablokb mentioned, even if not of any use for your current program it may come in handy for some future project.

1 Like