# Running parallel nodes with asynchronous code

**URL:** <https://discourse.nodered.org/t/running-parallel-nodes-with-asynchronous-code/48588>\
**Category:** General\
**Created:** [19 July 2021 12:28 UTC](https://discourse.nodered.org/t/running-parallel-nodes-with-asynchronous-code/48588 "2021-07-19T12:28:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![louislefevre](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/louislefevre/32/45176_2.png) [@louislefevre](https://discourse.nodered.org/u/louislefevre)\
**Post date:** [19 July 2021 12:28 UTC](https://discourse.nodered.org/t/running-parallel-nodes-with-asynchronous-code/48588/1 "2021-07-19T12:28:35Z")

</div>

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.

 ![forum3](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/5/358b1cb133944a817bbc4aac926c304aec055072.png)

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/6/664b97c1dda2f218aaeff51d9a88d68347547daa.png)

However, this is not working as expected. The following demonstrates the order in which the code blocks are executing.  
 ![forum2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/0/80252875ad682cb4c73f7380aeab545b48bc4dbb.png)

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](https://nodered.org/blog/2019/08/16/going-async) and shown in this image.  
 ![parallel-flow](https://nodered.org/blog/content/images/2019/08/flow2-async.svg)

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [19 July 2021 12:42 UTC](https://discourse.nodered.org/t/running-parallel-nodes-with-asynchronous-code/48588/2 "2021-07-19T12:42:04Z")

</div>

Put them in series rather than in parallel.

---

<div class="post-metadata">

**Author:** ![louislefevre](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/louislefevre/32/45176_2.png) [@louislefevre](https://discourse.nodered.org/u/louislefevre)\
**Post date:** [19 July 2021 12:43 UTC](https://discourse.nodered.org/t/running-parallel-nodes-with-asynchronous-code/48588/3 "2021-07-19T12:43:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [19 July 2021 12:46 UTC](https://discourse.nodered.org/t/running-parallel-nodes-with-asynchronous-code/48588/4 "2021-07-19T12:46:25Z")

</div>

> [@louislefevre](#):
>
> 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?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [19 July 2021 14:59 UTC](https://discourse.nodered.org/t/running-parallel-nodes-with-asynchronous-code/48588/5 "2021-07-19T14:59:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [19 July 2021 21:31 UTC](https://discourse.nodered.org/t/running-parallel-nodes-with-asynchronous-code/48588/6 "2021-07-19T21:31:12Z")

</div>

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

> **[node-red-contrib-actionflows](https://flows.nodered.org/node/node-red-contrib-actionflows)**
>
> Create extendable, loopable, and reusable design patterns for flows.

Craig

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [17 September 2021 21:31 UTC](https://discourse.nodered.org/t/running-parallel-nodes-with-asynchronous-code/48588/7 "2021-09-17T21:31:53Z")

</div>

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