how can non-blocking socket instances be implemented
how can non-blocking socket instances be implemented
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()
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
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.
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
Thanks but none of the class example uses sockets
Re: how can non-blocking socket instances be implemented
Thanks but none of the above examples use sockets.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: how can non-blocking socket instances be implemented
This example illustrates reading from, and writing to, a nonblocking socket.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: how can non-blocking socket instances be implemented
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 , 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()
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 , 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()
-
- Posts: 1
- Joined: Sun Oct 07, 2018 6:06 pm
Re: how can non-blocking socket instances be implemented
Thanks zedtech for the above. Exactly what I was looking for and works like a charm.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: how can non-blocking socket instances be implemented
You might like to look at the docs for uselect for a more efficient approach than using select.select().
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: how can non-blocking socket instances be implemented
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?).
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...
...