Run continuous code

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)

You can run long running python scripts like daemons with 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:

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

1 Like