Page 1 of 2

how can non-blocking socket instances be implemented

Posted: Wed Jan 03, 2018 7:02 am
by zedtech
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()

Re: how can non-blocking socket instances be implemented

Posted: Wed Jan 03, 2018 12:15 pm
by jomas

Re: how can non-blocking socket instances be implemented

Posted: Wed Jan 03, 2018 12:29 pm
by fdushin
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.

Re: how can non-blocking socket instances be implemented

Posted: Fri Jan 05, 2018 3:47 am
by zedtech
Thanks but none of the class example uses sockets

Re: how can non-blocking socket instances be implemented

Posted: Fri Jan 05, 2018 3:49 am
by zedtech
Thanks but none of the above examples use sockets.

Re: how can non-blocking socket instances be implemented

Posted: Fri Jan 05, 2018 8:24 am
by pythoncoder
This example illustrates reading from, and writing to, a nonblocking socket.

Re: how can non-blocking socket instances be implemented

Posted: Fri Jan 19, 2018 2:41 am
by zedtech
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 36178 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()

Re: how can non-blocking socket instances be implemented

Posted: Sun Oct 07, 2018 6:08 pm
by worldofchris
Thanks zedtech for the above. Exactly what I was looking for and works like a charm.

Re: how can non-blocking socket instances be implemented

Posted: Mon Oct 08, 2018 5:23 am
by pythoncoder
You might like to look at the docs for uselect for a more efficient approach than using select.select().

Re: how can non-blocking socket instances be implemented

Posted: Mon Oct 22, 2018 10:31 pm
by jarekd
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...
	...