Run async functions as tasks

Introduction

The task decorator allows for the integration of asyncio within tasks, letting users control asynchronous functions.

import asyncio

from aiida import load_profile
from aiida_workgraph import task

load_profile()
Profile<uuid='2c6af82ef2b241408a4c27c7c32d5490' name='presto'>

Use the async function to make it non‑blocking:

@task
async def add_async(x, y, time=5):
    print(f'Sleeping for {time} seconds...')
    await asyncio.sleep(time)
    return x + y


@task
def multiply(x, y):
    return x * y


@task.graph
def SumGraph(x, y):
    sum1 = add_async(x, y, time=5).result
    sum2 = add_async(x, y, time=5).result
    return multiply(sum1, sum2).result


SumGraph.run(1, 2)
06/29/2026 09:09:35 AM <2622> aiida.orm.nodes.process.workflow.workchain.WorkChainNode: [REPORT] [159|WorkGraphEngine|continue_workgraph]: tasks ready to run: add_async,add_async1
06/29/2026 09:09:35 AM <2622> aiida.orm.nodes.process.workflow.workchain.WorkChainNode: [REPORT] [159|WorkGraphEngine|on_wait]: Process status: Waiting for child processes: 160, 161
Sleeping for 5 seconds...
Sleeping for 5 seconds...
06/29/2026 09:09:41 AM <2622> aiida.orm.nodes.process.workflow.workchain.WorkChainNode: [REPORT] [159|WorkGraphEngine|update_task_state]: Task: add_async, type: PYFUNCTION, finished.
06/29/2026 09:09:41 AM <2622> aiida.orm.nodes.process.workflow.workchain.WorkChainNode: [REPORT] [159|WorkGraphEngine|continue_workgraph]: tasks ready to run:
06/29/2026 09:09:41 AM <2622> aiida.orm.nodes.process.workflow.workchain.WorkChainNode: [REPORT] [159|WorkGraphEngine|on_wait]: Process status: Waiting for child processes: 161
06/29/2026 09:09:41 AM <2622> aiida.orm.nodes.process.workflow.workchain.WorkChainNode: [REPORT] [159|WorkGraphEngine|update_task_state]: Task: add_async1, type: PYFUNCTION, finished.
06/29/2026 09:09:41 AM <2622> aiida.orm.nodes.process.workflow.workchain.WorkChainNode: [REPORT] [159|WorkGraphEngine|continue_workgraph]: tasks ready to run: multiply
06/29/2026 09:09:42 AM <2622> aiida.orm.nodes.process.workflow.workchain.WorkChainNode: [REPORT] [159|WorkGraphEngine|update_task_state]: Task: multiply, type: PYFUNCTION, finished.
06/29/2026 09:09:42 AM <2622> aiida.orm.nodes.process.workflow.workchain.WorkChainNode: [REPORT] [159|WorkGraphEngine|continue_workgraph]: tasks ready to run:
06/29/2026 09:09:42 AM <2622> aiida.orm.nodes.process.workflow.workchain.WorkChainNode: [REPORT] [159|WorkGraphEngine|finalize]: Finalize workgraph.

{'result': <Int: uuid: d8d6592e-dd3e-413e-b333-ff686281426f (pk: 165) value: 9>}

Note the order of execution. The async tasks run concurrently (both print the sleep message immediately). Even though each sleeps for 5 seconds, they both complete around the same time (note the timestamps). Since the multiply task depends on both, it waits for both to finish before executing (note the timestamps).

Summary

In this section, we’ve explored the task decorator for integrating asynchronous functions within tasks.

Total running time of the script: (0 minutes 7.841 seconds)

Gallery generated by Sphinx-Gallery