
how can non-blocking socket instances be implemented
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: how can non-blocking socket instances be implemented
To register a socket you need an event mask. Check the docs again 

Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: how can non-blocking socket instances be implemented
this is what I assume a library of umqtt isn't it? It's so complicated that I cannot follow ......pythoncoder wrote: ↑Fri Jan 05, 2018 8:24 amThis example illustrates reading from, and writing to, a nonblocking socket.


Re: how can non-blocking socket instances be implemented
can I only use select with stream? not DGRAM? what are the difference ?
many thanks.
many thanks.
Re: how can non-blocking socket instances be implemented
It also works with SOCK_DGRAM.
Here a testscript:
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...