https://prefect.io logo
Title
t

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


@task
async def async_task():
    return "async_task"


@task
def synchronous_task():
    return "synchronous_task"


@flow
async def my_flow():
    logger = get_run_logger()
    <http://logger.info|logger.info>("Executing async task:")
    async_result = await async_task()
    <http://logger.info|logger.info>(f"Result of async task was {async_result}")
    <http://logger.info|logger.info>("Executing synchronous task")
    synchronous_result = synchronous_task()
    <http://logger.info|logger.info>(f"Result of synchronous task was {synchronous_result}")
d

Dmytro Ponomarchuk

01/05/2023, 9:50 AM
Hi Torstain, You would need to add the
.submit()
function to call the async task. According to https://docs.prefect.io/tutorials/execution/#asynchronous-execution
t

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.
n

Nate

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
.submit()
, you can await async tasks in your flow like a normal async function. The only time you really need
submit()
is when you want to leverage a specific task runner's style of execution
t

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