# Nested invokeAsynchronous and invokeLater

**URL:** <https://forum.inductiveautomation.com/t/nested-invokeasynchronous-and-invokelater/26006>\
**Category:** Ignition\
**Created:** [June 21, 2019, 10:08am UTC](https://forum.inductiveautomation.com/t/nested-invokeasynchronous-and-invokelater/26006 "2019-06-21T10:08:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ecan](https://avatars.discourse-cdn.com/v4/letter/e/8edcca/32.png) [@ecan](https://forum.inductiveautomation.com/u/ecan)\
**Post date:** [June 21, 2019, 10:08am UTC](https://forum.inductiveautomation.com/t/nested-invokeasynchronous-and-invokelater/26006/1 "2019-06-21T10:08:15Z")

</div>

Let’s say I have functions `long_taking_function` and `long_taking_function2`. Both take a long time. Is it considered bad practice to have ‘nested’ `invokeAsynchronous` and `invokeLater`? If so, how can I improve this?

```auto
def long_process():
    bla = long_taking_function()
    if bla:
        def send_back():
            if bla:
                def long_process2():
                    bla2 = long_taking_function2()
                    def send_back2():
                        if bla2:
                            ...
                        else:
                            ...
                    system.util.invokeLater(send_back2)
                system.util.invokeAsynchronous(long_process2)
            else:
                ...
        system.util.invokeLater(send_back)
    else:
        ...
system.util.invokeAsynchronous(long_process)

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [June 21, 2019, 11:07am UTC](https://forum.inductiveautomation.com/t/nested-invokeasynchronous-and-invokelater/26006/2 "2019-06-21T11:07:11Z")

</div>

You can nest as shown. Another possibility would be to use just one asynchronous task and using CompletableFutures to pause that background and retrieve information from the foreground. See this discussion involving my later.py script module:

> [@Update GUI via Asynchronous thread](http://forum.inductiveautomation.com/t/update-gui-via-asynchronous-thread/9666/11):
>
> OK. I’ve poked myself with the futures idea. Grab the latest version of [later.py](https://www.automation-pros.com/ignition/later.py), then try this: def asyncInvoked(): # do something in the background shared.later.callLater(system.gui.messageBox, "Please Press OK").get() # do something after OK is pressed system.util.invokeAsynchronous(asyncInvoked) Note the use of .get() to wait for system.gui.messageBox to complete. This updated later.py includes an emulation of invokeLater for gateway scope. Untested and unsupported! Edit: Realized…

---

<div class="post-metadata">

**Author:** ![ecan](https://avatars.discourse-cdn.com/v4/letter/e/8edcca/32.png) [@ecan](https://forum.inductiveautomation.com/u/ecan)\
**Post date:** [June 21, 2019, 11:39am UTC](https://forum.inductiveautomation.com/t/nested-invokeasynchronous-and-invokelater/26006/3 "2019-06-21T11:39:08Z")

</div>

I love how responsive you and how useful your answers always are. Thanks a lot!
