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.
zedtech
Posts: 22
Joined: Thu Nov 30, 2017 3:36 am

how can non-blocking socket instances be implemented

Post by zedtech » Wed Jan 03, 2018 7:02 am

Sorry if this question has been asked before. My goal is to run a webserver on ESP8266 and at the same time do some else. For multitasking im using uasyncio module. Here is the snippet i have come up with so far:
import network
import usocket as socket
station = network.WLAN(network.STA_IF)
station.active(True)
station.connect('xxxx', 'xxxxxxxxxx')

#webserver
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setblocking(False) #non blocking
s.bind((' ',4045))
s.listen()
How can i successfully check for a connection, and if any receive data doing this repeatedly and at the time doing something else?
In the case of blocking mode, below is how i check for connection and receive data
s.setblocking(True)
while True:
conn, addr = s.accept()
request = conn.recv(1024)
reply = 'Ok'
conn.send(reply)
conn.close()

jomas
Posts: 9
Joined: Mon Dec 25, 2017 1:48 pm
Location: Netherlands

Re: how can non-blocking socket instances be implemented

Post by jomas » Wed Jan 03, 2018 12:15 pm


User avatar
fdushin
Posts: 32
Joined: Thu Jul 21, 2016 5:38 pm

Re: how can non-blocking socket instances be implemented

Post by fdushin » Wed Jan 03, 2018 12:29 pm

Here is another example of a generic TCPServer using uaysncio:

https://github.com/fadushin/esp8266/blo ... __.py#L354

(though I am having issues with uasyncio later than v1.3)

I have found usasyncio the best way to do cooperative multi-tasking in micropython. Here is a task-oriented base class and a few instances that extend it.

https://github.com/fadushin/esp8266/blo ... re/task.py

Basically, the idea is to break up your application into a series of cooperative tasks, which get invoked periodically by the uasyncio event loop.

zedtech
Posts: 22
Joined: Thu Nov 30, 2017 3:36 am

Re: how can non-blocking socket instances be implemented

Post by zedtech » Fri Jan 05, 2018 3:47 am

Thanks but none of the class example uses sockets

zedtech
Posts: 22
Joined: Thu Nov 30, 2017 3:36 am

Re: how can non-blocking socket instances be implemented

Post by zedtech » Fri Jan 05, 2018 3:49 am

Thanks but none of the above examples use sockets.

User avatar
pythoncoder
Posts: 3060
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: how can non-blocking socket instances be implemented

Post by pythoncoder » Fri Jan 05, 2018 8:24 am

This example illustrates reading from, and writing to, a nonblocking socket.
Peter Hinch

zedtech
Posts: 22
Joined: Thu Nov 30, 2017 3:36 am

Re: how can non-blocking socket instances be implemented

Post by zedtech » Fri Jan 19, 2018 2:41 am

To whom this may concern! This is just an update to the question i asked, particularly how to accept a socket that is blocking, to which recently I found a solution using select and hope this will be beneficial to someone else. Below is the solution
import usocket as socket
import uselect as select

def do_something_else():
#The other script to be executed besides of the blocking socket

def Client_handler(client_obj):
#Do this when there's a socket connection

server = socket.socket(socket.AF_INET
reply.jpg
reply.jpg (77.99 KiB) Viewed 1092 times
, socket.SOCK_STREAM)
server.bind(('', 5000))
server.listen(5)

while True:
r, w, err = select.select((server,), (), (), 1)
if r:
for readable in r:
client, client _addr = server.accept()
try:
Client_handler(client)
except OSError as e:
pass
do_something_else()

worldofchris
Posts: 1
Joined: Sun Oct 07, 2018 6:06 pm

Re: how can non-blocking socket instances be implemented

Post by worldofchris » Sun Oct 07, 2018 6:08 pm

Thanks zedtech for the above. Exactly what I was looking for and works like a charm.

User avatar
pythoncoder
Posts: 3060
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: how can non-blocking socket instances be implemented

Post by pythoncoder » Mon Oct 08, 2018 5:23 am

You might like to look at the docs for uselect for a more efficient approach than using select.select().
Peter Hinch

jarekd
Posts: 13
Joined: Mon Aug 21, 2017 3:54 pm

Re: how can non-blocking socket instances be implemented

Post by jarekd » Mon Oct 22, 2018 10:31 pm

Peter,
I'm trying to replace select.select with poll method, but without success... After calling p.ipool() script just stop working (waiting for something?).

Code: Select all

# s - my socket object

p = select.poll()
p.register(s)
while True:
	a, b = p.ipoll()   # stops here...
	...

Post Reply