arnauorriols / node-red-contrib-python-function

Write Python functions in Node-RED!
MIT License
41 stars 23 forks source link

Can not send multiple sequential messages #18

Open mecworks opened 1 year ago

mecworks commented 1 year ago

I need to write a script that continuously sends out messages every second that contain generated data. This module will only send out messages after the entire script has completed. For example, this fails as it waits continually:

import time
while True:
    MycustomData = somefunc()
    msg = {'payload': MycustomData}
    node.send(msg)
    time.sleep(1)

Is there a way to send messages continually? In my case, the somefunc() takes a long time to set up external hardware devices before it can retrieve data. It should be started only once to initialize everything then only queried for data when needed which would then send a new msg.

I think the way that code works now, I would have to run the setup of my instrumentation and all the overhead that comes with that every time I need a message sent. Is there a way to solve this?

Ideally, I should be able to send messages in a loop and have them sent out immediately when node.send() is called in which case, this node would never actually exit; it would just continually send messages.

krambriw commented 1 year ago

The code for this node has not been updated since several years Myself, I tried something similar earlier with this node but I gave up and stopped struggeling. Instead I run all my Python scripts next to Node-RED and communicate with them via MQTT. I start the scripts with the exec node. Works perfect and you can leverage on all the power of Python like multi-threading etc

mecworks commented 1 year ago

The exec node will allow sending multiple messages while it's running. This node needs to be updated to support the same type of functionality.