arnauorriols / node-red-contrib-python-function

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

time.sleep not working #10

Closed krambriw closed 3 years ago

krambriw commented 4 years ago

Can U give explanation why this code seems not working? I do need a kind of loop that keeps the code running

import time

while True:
    node.warn('hi')
    time.sleep(3)
arnauorriols commented 4 years ago

It behaves as I would expect it to behave. as soon as a message is sent to the node, a "hi" is output in the logs of the node-red service every 3 seconds indefinitely.

What are your expectatives?

krambriw commented 4 years ago

My problem is it seems not working for me, I see nothing in the debug window, I have tried node.log, node.warn, node.send but nothing is sent out from the node II think the loop may run too fast, entering into sleep to quickly, not giving node-red or node code chance to show/send the message in the debug window. When testing I have sometimes seen many "hi" messages suddenly in the debug window when breaking the loop, i.e. when making a change to the code and applying again

The following simple node-red example is not working for me, I would expect messages to be seen in the debug window

import time abort = False if (msg['payload'] == "abort"): abort = True node.warn("aborting") node.warn("starting") while not abort: node.warn("hi") time.sleep(3) node.warn('stopped')

[{"id":"647bb4a4.57e1dc","type":"python-function","z":"dbc3addc.853ef","name":"","func":"import time\nabort = False\nif (msg['payload'] == \"abort\"):\n abort = True\n node.warn(\"aborting\")\nnode.warn(\"starting\")\nwhile not abort:\n node.warn(\"hi\")\n time.sleep(3)\nnode.warn('stopped')\n","outputs":1,"x":640,"y":230,"wires":[["8516aec.f35e15"]]},{"id":"223f6941.c77dc6","type":"inject","z":"dbc3addc.853ef","name":"Start","topic":"","payload":"go","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":400,"y":230,"wires":[["647bb4a4.57e1dc"]]},{"id":"ae779dc4.edcad","type":"inject","z":"dbc3addc.853ef","name":"Abort","topic":"","payload":"abort","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":400,"y":300,"wires":[["647bb4a4.57e1dc"]]},{"id":"8516aec.f35e15","type":"debug","z":"dbc3addc.853ef","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":830,"y":230,"wires":[]}]

image

krambriw commented 4 years ago

I made another test, writing to a file instead. Only after I stopped the node execution manually (by changing anything in the node python code), all messages where flushed to the file. But during execution no messages where written to the file. Seems to be some "lock", preventing messages to be sent out from the node I'm running this in a RPi3+

krambriw commented 4 years ago

Additional information; the first example actually "works" kind of. If you click "Start" and then "Abort" and wait "long" time, suddenly a lot of "hi" is shown in the debug window in one shot but not one-by-one as expected. So some kind of "locking" is preventing sending messages from node one-by-one