# Run continuous code

**URL:** <https://discourse.nodered.org/t/run-continuous-code/39058>\
**Category:** General\
**Created:** [13 January 2021 13:36 UTC](https://discourse.nodered.org/t/run-continuous-code/39058 "2021-01-13T13:36:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![votecoffee](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/votecoffee/32/31932_2.png) [@votecoffee](https://discourse.nodered.org/u/votecoffee)\
**Post date:** [13 January 2021 13:36 UTC](https://discourse.nodered.org/t/run-continuous-code/39058/1 "2021-01-13T13:36:19Z")

</div>

I want to run python3 code that is continuous (for camera frame grabbing). NodeRed is my main control program, so looking for a seamless way to integrate. Ultimately I am pushing the result image to mqtt or a webpage (doing MQTT using Paho and webpage using ImageZMQ right now).

Is there a way to do one of the following:

- Run continuous code using python3 function, returning control to NodeRed between loops
- Start/stop separate application from within NodeRed (maybe with python3 node returning process handle, ZMQ or MQTT to get image/result packet into NodeRed)

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [13 January 2021 13:47 UTC](https://discourse.nodered.org/t/run-continuous-code/39058/2 "2021-01-13T13:47:04Z")

</div>

You can run long running python scripts like daemons with [node-red-node-daemon](https://flows.nodered.org/node/node-red-node-daemon).  
This allows you to communicate with the process via stdin and receive results via stdout.  
A very basic example would be to use something like:

```auto
while True:
    line = sys.stdin.readline()
    on_message(line.rstrip())

```

In your python script to read what is send to the input of the daemon node in nodered. This would trigger the on\_message function in your python script on every input to the node.  
Another way would be to run your python code as a service and communicate with nodered completely via mqtt as you are already using it anyway. Those are just two ways that I can think of of the top of my head.

Johannes

---

<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:** [27 January 2021 13:47 UTC](https://discourse.nodered.org/t/run-continuous-code/39058/3 "2021-01-27T13:47:20Z")

</div>

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