If unlucky two events happen at same time

Hi! I have a function with multi input and many "if" to produce the desidered msgpayload to send after to hardware. If unfortunately another message come to this function when is escecuting his code, will this message be discard?
If node red uses only one thread/core probably a message can't come when it is doing something else, but if there is multi-thread core maybe is possible. In this case the message will be waiting for the function to be finished, and after will be processed (I wont' loose nothing) or chances are it will be discarded?

I have some important events that may occour once without the chance of repeat them, I would like to be sure they will not be ignored. Thank you

I'm thinking to use another approach, using variables stored in RAM. I'll use a cycling injection to execute the function every 50-100ms, the function will copy to hardware what it finds in memory or scan buttons and copy in memory, i think slower, but more reliable solution.

Node-RED (and node.js) are single threaded so you don't need to worry about concurrency. If a message arrives at a node when it is still handling the previous message then the message will be queued until the node gets round to handling it. No messages will be discarded.

Every message gets a unique ID as one of its object properties.

That is 98% correct. The missing piece is that if the node does any asynchronous work (such as reading/writing to the network), it will yield its hold on the node.js thread whilst that work happens - which may allow the node to start processing the second message.

Some nodes will include internal code to ensure each message is handled in its entirety before the next one is handled - but that will be due to a specific requirement of the node to do that. For example, the Switch node can include a rule to compare against the previous value received - so the messages must be handled completely one at a time.

thank you!

mates, for some reasons I prefered the solution with memory variables (it outputs what is in memory), now I have a function that needs to be called as fastest as possible, but the inject can't be less than 1 second. Can I execute a function more often?

Do you mean you are trying to poll inputs instead of letting them trigger when they have something to say? If so then don't do it by polling, go back to plan A.
However if you need sub 10ms precision on the timing of things then you are unlikely to achieve that with node-RED, though the power of the processor comes into this of course.

It can be set to 0.1 seconds if you want to try to get a smaller interval - but I wouldn't rely on it being accurate once you get below a second.

I see I was confusing this post with another, which was having trouble trying to do very rapid injection, so my comment may not be correct. However my basic point is still valid. If you have some inputs that can automatically trigger then it is usually better to use that feature rather than waste large amounts of processor time polling.

thank you, 0.1 second will be enough for me, ok it works ) only 2-3% more of CPU have come.
Yes unfortunately I've external I/O inputs that can't generate anything, I need to polling them.

The pattern we use elsewhere in some of the GPIO nodes is to run a separate python process that monitors the state of the inputs and have them write to stdout when something happens. That script can be run using the exec node so node-red is only triggered when it needs to be. Maybe something to look at if you need to offload some of that processing.

1 Like

thank you for kind reply, unfortunately there are inputs read by I2C bus, I think I can't do much. ANyway controlling them about every 0,1 second is ok