Node-Red -Blocking and non Blocking- Synchronous and Asynchronous

With most programs functions are blocking in that they will complete their task before returning.

Generally this type of programming is easy to manage as you have only one task active at any time.

This style of programming is known as synchronous, but  the problem with it is that it can make applications very slow as they have to continually wait.





This is very noticeable when the functions are working with slow media like disks and networks.

Non Blocking functions use asynchronous style of programming were functions are started and return immediately so that the application can process the next task.

The function that was started continues in the background and notifies the main application when it has completed its task.

Here’s an diagram illustrating the program flow of each:sync-async-blocking-non-blocking-functions

Using the above diagram imagine we read some text from a file in function 1 and at the end we printed the text program ended.

for the synchronous case we would see

text from function 1
result of function 2
result of function 3
Text-end program

for the Asynchronous case we would see:
Text-end program
result of function x
result of function x
result of function x

Summary

  1. Blocking Functions:
    • Blocking functions  pause the execution of the program until they complete their task.
    • Execution continues one the blocking function returns
  2. Non-Blocking Functions:
    • Non-blocking functions  do not halt the execution of the program but return immediately.
    • Execution continues with next task.
    • Results are processed when tasks complete and in no particular order

How do Asynchronous Programs work

Asynchronous programs use an event loop to manage asynchronous operations.

An event loop is an endless loop (while loop), which runs and processes tasks that are added to the event queue.

Each task runs to completion before it processes another task from the event queue. See

Node-Red and the Event Loop

Node-red is a single threaded application that uses asynchronous functions which are non blocking.

However functions that you write yourself probably will be blocking which is why you need to keep these functions small and not do a lot of heavy data processing in them.

A long running blocking function node anywhere in the node-red workspace will slow down the entire workspace.



Related Tutorials and Resources

Click to rate this post!
[Total: 0 Average: 0]

Leave a Reply

Your email address will not be published. Required fields are marked *