Torstein Molland

01/05/2023, 9:34 AM
Hi, Do all tasks within an asynchronous flow need to be asynchronous, or can synchronous tasks be run within an asynchronous flow? I've attached a minimal example and the output from the Prefect UI. The asynchronous task runs successfully, but the synchronous task seems to hang indefinitely. Any help is greatly appreciated!
from prefect import flow, get_run_logger, task

async def async_task():
    return "async_task"

def synchronous_task():
    return "synchronous_task"

async def my_flow():
    logger = get_run_logger()
    <|>("Executing async task:")
    async_result = await async_task()
    <|>(f"Result of async task was {async_result}")
    <|>("Executing synchronous task")
    synchronous_result = synchronous_task()
    <|>(f"Result of synchronous task was {synchronous_result}")

Dmytro Ponomarchuk

01/05/2023, 9:50 AM
Hi Torstain, You would need to add the
function to call the async task. According to

Torstein Molland

01/05/2023, 9:57 AM
Hi Dmytro, Thank you for the reply. This does not seem to answer my issue. I'm having trouble executing a synchronous task within an asynchronous flow. The asynchronous task runs successfully, but the synchronous task hangs indefinitely. The doc-entry you linked to deals with issues executing asynchronous tasks and not with executing synchronous tasks within asynchronous flows.


01/05/2023, 4:42 PM
Hi @Torstein Molland - this issue is on our radar and a fix is on the roadmap I would recommend calling async tasks from async flows and sync tasks from sync flows for now. If you run into specific use-case where this is a blocker, feel free to raise that here and we can likely find a solid workaround for you. Note that it's not strictly true (as suggested above) that async tasks must be called with
, you can await async tasks in your flow like a normal async function. The only time you really need
is when you want to leverage a specific task runner's style of execution

Torstein Molland

01/05/2023, 4:46 PM
Thank you kindly for the reply, Nate. I'll use the workaround in the meantime as it seems to solve my issue 😊
👍 1