A question about a 'while True' loop #13541
Replies: 2 comments 1 reply
-
I wonder if: if True:
client.wait_msg() is an idiom, somehow evaluating the return value of |
Beta Was this translation helpful? Give feedback.
-
The code snippet shown is confusing because the comments have been removed. The original is from "lib/micropython-lib/micropython/umqtt.simple/example_sub.py": ...
while True:
if True:
# Blocking wait for message
c.wait_msg()
else:
# Non-blocking wait for message
c.check_msg()
# Then need to sleep to avoid 100% CPU usage (in a real
# app other useful actions would be performed instead)
time.sleep(1)
... The example forces the use of "blocking wait" and does nothing else. To do other processing, the "non-blocking wait" is provided. Have a look at "lib/micropython-lib/micropython/umqtt.simple/umqtt/simple.py" to see how this is done. The callback is called in wait_msg(). A very clever way of using sock.setblocking(); False in check_msg() and True in wait_msg(), after sock.read(1). This does a "non-blocking wait" if no data is available and processes the incoming message if data is available. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi all,
I've been exploring MQTT and I came across some code that puzzles me.
The code is here: https://www.emqx.com/en/blog/micro-python-mqtt-tutorial-based-on-raspberry-pi#subscribe
(See below)
The part I don't understand is this:
How can the 'else' condition ever be met?
While True would mean that 'True' will always be true right?
And what is even being evaluated here, what should be true?
I'm sorry for sounding confused but true can't be false right?
My mind is stuck in a loop now :)
If someone can explain, please do.
Thanks.
========================
Complete code (without wifi connect)
Beta Was this translation helpful? Give feedback.
All reactions