UDP server: why the main loop doesn't block the program? #15427
-
Hi guys, Sorry for this probably dumb question. I'm using an M5 Stack ESP32 board, and I created a quick "hello world" style program that listens for data on UDP. Here it is: from m5stack import *
from m5ui import *
from uiflow import *
import socket
setScreenColor(0x111111)
label0 = M5TextBox(13, 9, "label0", lcd.FONT_Default, 0xFFFFFF, rotate=0)
def buttonA_wasPressed():
# global params
label0.setText('Hello M5')
pass
btnA.wasPressed(buttonA_wasPressed)
udpsocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udpsocket.bind(('0.0.0.0', 5000))
while True:
print(udpsocket.recv(1024))
wait_ms(2) I'm using the "Ui Flow" web IDE of M5 Stack which has goodies such as a WYSIWYG editor for the screen of the M5 Stack device. Hence My question: my understanding is that the Any hints? Thanks 🙂 |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 6 replies
-
I would put into: while True:
print(udpsocket.recv(1024))
wait_ms(2)
|
Beta Was this translation helpful? Give feedback.
-
I performed the modifications. I confirm that the line |
Beta Was this translation helpful? Give feedback.
-
I guess the following method adds an interrupt service routine to the GPIO: This works even, if your main thread is blocked. This interrupts the main thread and the ISR is called and after this, the main thread continues the work. But you can test it easily: from m5stack import *
from m5ui import *
from uiflow import *
setScreenColor(0x111111)
label0 = M5TextBox(13, 9, "label0", lcd.FONT_Default, 0xFFFFFF, rotate=0)
def buttonA_wasPressed():
label0.setText('Hello M5')
btnA.wasPressed(buttonA_wasPressed)
# busy loop
while True:
pass If then a button press still changes the text, then it's an ISR. |
Beta Was this translation helpful? Give feedback.
-
Thanks! I'd say it certainly looks like an ISR. I worked w/ ISRs in the past, on an ESP8266. I recall that code within an ISR is very very sensible. E.g. should be very quick; shouldn't modify the stack or something like that, etc. 1/ The doc was really explicit on this matter. 2/ I also got the taste of it. Was performing some string concatenations that would generate subtle corruptions of my app. My hesitation came from the following fact: the WYSIWYG editor makes use of such handlers pretty nonchalantly. There are no warnings about what operations should or shouldn't be done in such handlers. So I make the assumptions that they cannot be ISRs. So the current hypothesis is that: the handlers are indeed ISRs. And probably that building apps a bit more heavier w/ the M5 stack WYSIWYG editor would probably do infringe the rules of ISR, generating strange runtime bugs. |
Beta Was this translation helpful? Give feedback.
I guess the following method adds an interrupt service routine to the GPIO:
btnA.wasPressed(buttonA_wasPressed)
This works even, if your main thread is blocked. This interrupts the main thread and the ISR is called and after this, the main thread continues the work.
But you can test it easily:
If then a button press still changes the text, then it's an ISR.