Python while cycle don't run in node-red

Hello why in python3 function this program run only one time, outside node-red he run as per expected.

rom rpiplc_lib import rpiplc
    valore= rpiplc.digital_read("I0.0")
    if valore == 0:
        msg['payload'] = True
    else :
        msg['payload'] = False

in the debug I would expect to get a continuous change between true and false or a continuous false or true, but it runs only once a time for injection then i have to inject it again.
What is wrong?

You've given us no information on how you try to run that Python program. Since Node-RED is natively Node.js (JavaScript), you will need to give us more information if you want some help.

Sorry you are right, i use python3 function that work well, but i don't understan what is the problem with this cycle:


Well part of the problem is that you didn't actually respond to my question which was about how you are running your Python code. Please take care to respond to forum responses with the information asked because it gets very tiring and difficult for people to help otherwise.

However, inadvertently, you've PARTIALLY shared the information anyway. You are using one of the contributed Python function nodes. But which one? There are at least 2, one of which hasn't been updated for over 3 years.

This is the package:

So I don't use that and I don't use Python that much either. However, one thing. If I were doing that in JavaScript, I wouldn't be using return msg inside a loop because I'm not sure that does what you think it does. I think it simply exits the loop?

You probably should either have a final return msg outside the loop or you would use node.send(msg) inside the loop if you wanted to send the output as it arrives.

But then again, you don't normally do an endless loop in a function node since it will probably block your flows (at least it would in a standard function I think).

Hello and thanks for your reply
With the node.send(msg) i never solve, but i use the repeat interval of the inject and i delete the while in the function, i don know if is the best solution but it works

Well, the main point is that the return msg is not doing what you think it is and it does not send a msg because it is returning from the loop not returning from the function node. You either have to use node.send - assuming the Python function node has implemented it - or you need to do another return msg outside the loop at the end.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.