Running parallel nodes with asynchronous code

I'm trying to run asynchronous code within nodes that run in parallel, where the following is an example of a flow which does this. The important parts are the Async 1 and Async 2 nodes.

These Async nodes are of the same type, and contain the following code which will run when they receive input. For context, the purpose of shell.execute() here is to run some Python code in a child process, the async function will wait for it to complete execution, and once it does the async function will continue.
forum1

However, this is not working as expected. The following demonstrates the order in which the code blocks are executing.
forum2

As shown, execution is flowing like so:

  • shell.execute(runner) executes for the Async 1 node.
  • shell.execute(runner) executes for the Async 2 node.
  • shell.execute(result) executes for the Async 1 node.
  • shell.execute(result) executes for the Async 2 node.
  • The Async 1 node ends.
  • The Async 2 node ends.

The order of execution is getting jumbled up; what I want is to execute the entire function before moving onto the next node. So under correct circumstances, execution would go like so:

  • shell.execute(runner) executes for the Async 1 node.
  • shell.execute(result) executes for the Async 1 node.
  • The Async 1 node ends.
  • shell.execute(runner) executes for the Async 2 node.
  • shell.execute(result) executes for the Async 2 node.
  • The Async 2 node ends.

I suspect the reason for this may be related to how Node-RED runs flows in parallel, as described here and shown in this image.
parallel-flow

So my question is, how can I modify my code so that it will finish executing Async 1 before beginning to execute Async 2?

Put them in series rather than in parallel.

Putting them in a series does work, but I need the code to be capable of handling parallel nodes.

Why? If you want something to start when something else is complete then what benefit do you get from putting them in parallel?

If what you are really trying to do is to protect the python script from being run more than once at any one time then it would probably be better to handle that within the script.

You might want to look at these nodes to help you out

Craig

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.