How to use non blocking functions, asynchronous tasks
Re: How to use non blocking functions, asynchronous tasks
@dexterlabora,
Hey I have a similar issue and I'm wondering if you have been able to find the fix and so if please share the solution.
Thanks in advance.
Hey I have a similar issue and I'm wondering if you have been able to find the fix and so if please share the solution.
Thanks in advance.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: How to use non blocking functions, asynchronous tasks
It is possible to use Socket instances in nonblocking mode. I'm afraid the only example I have is rather nontrivial, which is part of my resilient MQTT which is an asynchronous implementation of MQTT.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: How to use non blocking functions, asynchronous tasks
Yes, that's possible and when i do socket.setblocking(False) I'm getting the error "oserror:[errno 11] eagain
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: How to use non blocking functions, asynchronous tasks
You'll have to provide more details if we are to be able to help. See How to submit a bug report.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: How to use non blocking functions, asynchronous tasks
import uasyncio as uasyncio
import machine
import usocket as socket
import network
import ntptime
#connect to local wifi
stat = network.WLAN(network.STA_IF)
stat.active(True)
stat.connect('ssid','passwd')
#set socket webserver
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setblocking(False)
s.bind(('',4040))
s.listen(2)
loop = asyncio.get_event_loop()
settime()
async def loop1():
while True:
t = machine.RTC().datetime()
current_time = (3600*t[3] + 60*t[4] + t[5] #time in sec
await asyncio.sleep(0)
async def check_conn():
while True:
try:
conn, addr = s.accept()
print("Got a connection from %s " % str(addr))
request = conn.recv(1024)
print(request)
conn.close()
except OSError as error:
print('socket timed out')
await asyncio.sleep(0)
loop.create_task(loop1())
loop.create_task(check_conn())
loop.run_forever()
whenever i gett a connection, the program only prints "Got a connection from xxxxxxxx" and doesn't print the received data. What I am missing out?
import machine
import usocket as socket
import network
import ntptime
#connect to local wifi
stat = network.WLAN(network.STA_IF)
stat.active(True)
stat.connect('ssid','passwd')
#set socket webserver
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setblocking(False)
s.bind(('',4040))
s.listen(2)
loop = asyncio.get_event_loop()
settime()
async def loop1():
while True:
t = machine.RTC().datetime()
current_time = (3600*t[3] + 60*t[4] + t[5] #time in sec
await asyncio.sleep(0)
async def check_conn():
while True:
try:
conn, addr = s.accept()
print("Got a connection from %s " % str(addr))
request = conn.recv(1024)
print(request)
conn.close()
except OSError as error:
print('socket timed out')
await asyncio.sleep(0)
loop.create_task(loop1())
loop.create_task(check_conn())
loop.run_forever()
whenever i gett a connection, the program only prints "Got a connection from xxxxxxxx" and doesn't print the received data. What I am missing out?
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: How to use non blocking functions, asynchronous tasks
There are a few things wrong here. Firstly loop1 has a syntax error in the line which sets current_time. Secondly the loop doesn't do anything useful: it creates the local variable current_time, and assigns a value to it, but this isn't used anywhere in the code. I'm unsure of your intent, but perhaps current_time should be global?
But the reason the code doesn't retrieve data is that you declared the socket as nonblocking. So
is guaranteed to return immediately whether or not data is available by the time the line of code is executed. There are many examples on the web of how to use nonblocking sockets, but most of these assume a reliable connection. Or you could look at the code I wrote here - this routine is used in an asynchronous MQTT client for the ESP8266 and handles the error conditions which can arise over unreliable WiFi connection.
But the reason the code doesn't retrieve data is that you declared the socket as nonblocking. So
Code: Select all
request = conn.recv(1024)
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: How to use non blocking functions, asynchronous tasks
@pythoncoder,
Thanks for the comments. Yes, the current time isn't serving any particular purpose, but can easily provide one for it. My many focus is to have both functions run at the same. In socket blocking mode, only the check_conn() function is working. But i want both to run.
Thanks for the comments. Yes, the current time isn't serving any particular purpose, but can easily provide one for it. My many focus is to have both functions run at the same. In socket blocking mode, only the check_conn() function is working. But i want both to run.
Re: How to use non blocking functions, asynchronous tasks
Your project i was referenced too is partially helpful in that i couldn't find where you are catching the socket.accept(). How do i catch the socket.accept()?
Also, according to some reading i did, when the socket instance is set to non blocking and upon checking whether there's a connection and whether data has been received and if none of that happened then the socket is set to blocking mode. How true is this? And if its true, how can the non blocking mode remain enforced even when there's no connection and no data received?
I hope I'm not asking too much from you. But again thanks for your help.
Also, according to some reading i did, when the socket instance is set to non blocking and upon checking whether there's a connection and whether data has been received and if none of that happened then the socket is set to blocking mode. How true is this? And if its true, how can the non blocking mode remain enforced even when there's no connection and no data received?
I hope I'm not asking too much from you. But again thanks for your help.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: How to use non blocking functions, asynchronous tasks
My code is for a client so there is no accept. I offered it merely as an example of how to read and write from a nonblocking socket (while checking for errors which can occur on a wireless link).
As for sockets reverting to blocking mode this is outside my knowledge or experience.
As for sockets reverting to blocking mode this is outside my knowledge or experience.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: How to use non blocking functions, asynchronous tasks
Thanks for your time..