Periodic machine timer blocked while waiting socket.connect()
Hi,
I am using micropython version 1.19.1 on the esp32 platform
The problem is that when connecting to the socket, the timer is blocked.
After the socket connecting is interrupted, the callback function is called several times at a time
Here's a fragment of code where you can see the problem:
import socket
import network
from machine import Timer
import time
sta_if = network.WLAN(network.STA_IF)
if not sta_if.isconnected():
sta_if.active(True)
time.sleep_us(200)
sta_if.connect('xxx', 'xxx')
while not sta_if.isconnected():
time.sleep(1)
t = Timer(-1)
t.init(period=1000, mode=Timer.PERIODIC, callback=lambda t:print(time.time()))
def connect():
try:
print('connecting')
addr = socket.getaddrinfo('192.168.1.41', 1886)[0][-1]
s = socket.socket()
s.connect(addr)
s.send(bytes('.....\n', 'utf8'))
s.read()
s.close()
except Exception as e:
print(e)
time.sleep(10)
connect()
Output:
719797401
719797402
719797403
719797404
719797405
719797406
719797407
719797408
719797409
connecting
719797428
[Errno 113] ECONNABORTED
719797428
719797428
719797428
719797428
719797428
719797428
719797428
719797429
719797430
719797431
719797432
719797433
...
As you can see, time.time() should be printed every 1sec, but when you call socket.connect() it stops printing. When the connecting is interrupted, the function is suddenly called several times with the same result
periodic timer blocked while waiting socket read()
Hello,
the same code (see below) running on ESP8266 and ESP32 produces different results.
This code creates a periodic timer calling a simple print() at 5 sec intervals, opens a socket, sends some bytes and then waits for a reply.
Under ESP8266, the timer continues to run while the read() is blocking.
Under ESP32, the timer is "paused" while the read() is blocking :(
any idea ?
import socket
import network
from machine import Timer
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect('xxxx', 'xxxxx')
t = Timer(3)
t.init(period=5000, mode=Timer.PERIODIC, callback=lambda t:print("hello"))
def connect():
addr = socket.getaddrinfo('192.168.0.40', 4242)[0][-1]
s = socket.socket()
s.connect(addr)
s.send(bytes('.....\n', 'utf8'))
s.read()
s.close()
OUTPUT:
ESP8266:
hello
hello
>>> connect()
hello
hello
ESP32:
hello
hello
>>> connect()