# Python while cycle don't run in node-red

**URL:** <https://discourse.nodered.org/t/python-while-cycle-dont-run-in-node-red/74271>\
**Category:** General\
**Created:** [27 January 2023 18:18 UTC](https://discourse.nodered.org/t/python-while-cycle-dont-run-in-node-red/74271 "2023-01-27T18:18:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![superlogan72](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/superlogan72/32/74680_2.png) [@superlogan72](https://discourse.nodered.org/u/superlogan72)\
**Post date:** [27 January 2023 18:18 UTC](https://discourse.nodered.org/t/python-while-cycle-dont-run-in-node-red/74271/1 "2023-01-27T18:18:37Z")

</div>

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

```auto
rom rpiplc_lib import rpiplc
rpiplc.init("RPIPLC_58")
rpiplc.pin_mode("I0.0",rpiplc.INPUT)
while(True):
    valore= rpiplc.digital_read("I0.0")
    print(valore)
    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?  
Thanks  
Luca

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [27 January 2023 20:41 UTC](https://discourse.nodered.org/t/python-while-cycle-dont-run-in-node-red/74271/2 "2023-01-27T20:41:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![superlogan72](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/superlogan72/32/74680_2.png) [@superlogan72](https://discourse.nodered.org/u/superlogan72)\
**Post date:** [30 January 2023 09:14 UTC](https://discourse.nodered.org/t/python-while-cycle-dont-run-in-node-red/74271/3 "2023-01-30T09:14:15Z")

</div>

Sorry you are right, i use python3 function that work well, but i don't understan what is the problem with this cycle:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/6/76c9f025962b0040e47231f3a4b18a4ea763e918.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/0/b0170a475d3385dc44d9331e9897870c90380e5b.png)

Thanks

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [30 January 2023 09:59 UTC](https://discourse.nodered.org/t/python-while-cycle-dont-run-in-node-red/74271/4 "2023-01-30T09:59:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![superlogan72](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/superlogan72/32/74680_2.png) [@superlogan72](https://discourse.nodered.org/u/superlogan72)\
**Post date:** [30 January 2023 10:33 UTC](https://discourse.nodered.org/t/python-while-cycle-dont-run-in-node-red/74271/5 "2023-01-30T10:33:07Z")

</div>

This is the package:  
node-red-contrib-python3-function

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [30 January 2023 11:23 UTC](https://discourse.nodered.org/t/python-while-cycle-dont-run-in-node-red/74271/6 "2023-01-30T11:23:12Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![superlogan72](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/superlogan72/32/74680_2.png) [@superlogan72](https://discourse.nodered.org/u/superlogan72)\
**Post date:** [31 January 2023 09:55 UTC](https://discourse.nodered.org/t/python-while-cycle-dont-run-in-node-red/74271/7 "2023-01-31T09:55:44Z")

</div>

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  
Thanks

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [31 January 2023 20:39 UTC](https://discourse.nodered.org/t/python-while-cycle-dont-run-in-node-red/74271/8 "2023-01-31T20:39:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [1 April 2023 20:40 UTC](https://discourse.nodered.org/t/python-while-cycle-dont-run-in-node-red/74271/9 "2023-04-01T20:40:14Z")

</div>

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