how can non-blocking socket instances be implemented

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: how can non-blocking socket instances be implemented

Post by pythoncoder » Tue Oct 23, 2018 6:05 am

To register a socket you need an event mask. Check the docs again ;)
Peter Hinch
Index to my micropython libraries.

fishjoe
Posts: 7
Joined: Wed Aug 24, 2022 9:25 am
Location: Chrischurch

Re: how can non-blocking socket instances be implemented

Post by fishjoe » Sun Oct 09, 2022 5:44 am

pythoncoder wrote:
Fri Jan 05, 2018 8:24 am
This example illustrates reading from, and writing to, a nonblocking socket.
this is what I assume a library of umqtt isn't it? It's so complicated that I cannot follow ...... :shock: :shock:

fishjoe
Posts: 7
Joined: Wed Aug 24, 2022 9:25 am
Location: Chrischurch

Re: how can non-blocking socket instances be implemented

Post by fishjoe » Sun Oct 09, 2022 5:46 am

can I only use select with stream? not DGRAM? what are the difference ?

many thanks.

DeaD_EyE
Posts: 19
Joined: Sun Jul 17, 2022 12:57 pm
Contact:

Re: how can non-blocking socket instances be implemented

Post by DeaD_EyE » Mon Oct 10, 2022 12:13 pm

It also works with SOCK_DGRAM.

Here a testscript:

Code: Select all

import time
import uasyncio as asyncio
from uasyncio.stream import Stream

from socket import socket, AF_INET, SOCK_STREAM, SOCK_DGRAM, SOL_SOCKET, SO_REUSEADDR
from select import select


def with_select(ip="0.0.0.0", port=8080, stype=SOCK_DGRAM):
    interval = 1.0
    
    s = socket(AF_INET, stype)
    s.setsockopt(SOL_SOCKET, SO_REUSEADDR, True)
    
    if stype is SOCK_STREAM:
        print("Connecting")
        s.connect((ip, port))
        addr = (ip, port)
    else:
        print("Opening port")
        s.bind((ip, port))
        
    s.setblocking(False)

    while True:
        recv_sockets, *_ = select([s], [], [], interval)
        
        for read in recv_sockets:
            if stype is SOCK_DGRAM:
                data, addr = read.recvfrom(1024)
            else:
                data = read.recv(1024)
            
            if not data or data.startswith(b"END"):
                read.close()
                return
                
            print(addr, data.decode().rstrip())
        
        print("Doing other stuff...")
        
    print("Done")
    s.setblocking(True)
    s.close()


async def _alive():
    while True:
        print("Alive")
        await asyncio.sleep(1)


async def with_async_stream(ip="0.0.0.0", port=8080, stype=SOCK_DGRAM):
    # to check if eventloop still works
    # and is not blocked by something
    asyncio.create_task(_alive())
    
    s = socket(AF_INET, stype)
    s.setsockopt(SOL_SOCKET, SO_REUSEADDR, True)
    
    if stype is SOCK_STREAM:
        print("Connecting")
        s.connect((ip, port))
    else:
        print("Opening port")
        s.bind((ip, port))
        
    s.setblocking(False)
    stream = Stream(s)

    while True:
        # reading works
        data = await stream.read(1024)
        if not data or data.startswith(b"END"):
            break
        
        # await stream.awrite(data)
        #
        # this could not work, because UDP
        # is a connectionless protocol
        # the address is required, but Stream is not made
        # for UDP afik
        
        print(data.rstrip().decode())
        
    s.setblocking(True)
    s.close()


def run_with_async_stream():
    asyncio.run(with_async_stream(stype=SOCK_DGRAM))


with_select()

# does not work completly
# run_with_async_stream()

Opening port
Doing other stuff...
Doing other stuff...
Doing other stuff...
Doing other stuff...
('192.168.10.130', 37969) Test
Doing other stuff...
Doing other stuff...
Doing other stuff...

User avatar
Ventran
Posts: 24
Joined: Sun Jun 21, 2020 4:28 pm
Location: Poland, Europe

Re: how can non-blocking socket instances be implemented

Post by Ventran » Sat Nov 12, 2022 6:45 pm


Post Reply