I do get this warning ```RuntimeWarning: coroutine...
# prefect-getting-started
c
I do get this warning
Copy code
RuntimeWarning: coroutine 'run_task_async' was never awaited
  return fn(*args, **kwargs)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Probably related?
n
hi @cnsmyth - this is because the async task was never awaited! so the coroutine was destroyed when your code ended without ever running so you can either
await
your
run_task_async
or add
_sync=True
I will add a new entry for this specific case where the coro is not awaited but is also not used downstream
c
@Nate thanks for the input! I tried to
await
the function that calls
email_send_message
... but maybe I did that wrong. Do you have additional documentation I can read on await to ensure I implement that correctly?
n
https://docs.python.org/3/library/asyncio-task.html#awaitables as it relates to prefect: • all block methods are async, but with the ability to act sync in a sync context • if you use them in an async context (like inside a function like
async def some_async_fn
) then you must use them like async functions (prefect 2 magically did this for you, which caused other problems, so that's why you're seeing this now) • if you use block methods in a sync context, you don't need to await them, you can treat them like normal sync functions
d
Does this work with prefect_shell shell_run_command output = shell_run_command.with_options( name=f"shell_run_command-{var1}" )( command=SHELL_SCRIPT, return_all=True, stream_level=logging.DEBUG ) await doesnt seem to work and not sure where i put the _sync=True
n
shell_run_command
is just an async task, so it won't accept
_sync=True
like things that are decorated with
sync_compatible
(all of this sync / async stuff is a bit confusing, and we're working on clearing that up soon) > await doesnt seem to work can you explain?
d
Copy code
The output of shell_run_command: <coroutine object run_task_async at 0x7ffffb6da5c0>
/usr/local/lib/python3.12/json/decoder.py:353: RuntimeWarning: coroutine 'run_task_async' was never awaited
prefect_shell i believe is package controlled by prefect. Maybe im on an old version that needs updated
ok apparently prefect_shell was moved into prefect core https://github.com/PrefectHQ/prefect-shell?tab=readme-ov-file
n
yes!
ok apparently prefect_shell was moved into prefect core
and yep
The output of shell_run_command: <coroutine object run_task_async at 0x7ffffb6da5c0>
/usr/local/lib/python3.12/json/decoder.py353 RuntimeWarning: coroutine 'run_task_async' was never awaited
im guessing you're calling
shell_run_command
from within a sync function? like
def some_func()
d
Has this been updated to work with Prefect 3?
Inside a @task im running
Copy code
output = shell_run_command.with_options(
        name=f"shell_run_command-{name}"
    )(
        command=SHELL_SCRIPT,
        return_all=True,
        stream_level=logging.DEBUG
    )
    <http://logger.info|logger.info>(f"The output of shell_run_command: {output}")
n
yes it has - so because the
shell_run_command
function is async, you need to
await
it if you're in a sync context, use
Copy code
named_task = shell_run_command.with_options(
        name=f"shell_run_command-{name}"
    )
    output = asyncio.run(
      named_task(
        command=SHELL_SCRIPT,
        return_all=True,
        stream_level=logging.DEBUG
      )
    )
    logger.info(f"The output of shell_run_command: {output}")
if you're in an async context await the function
Copy code
output = await shell_run_command.with_options( # added await here
        name=f"shell_run_command-{name}"
    )(
        command=SHELL_SCRIPT,
        return_all=True,
        stream_level=logging.DEBUG
    )
    logger.info(f"The output of shell_run_command: {output}")
d
Thanks Nate! This is very helpful i was missing the asyncio.run(...) part
not sure where best to put that in the documentation but this is very helpful and glad i was able to search the slack channel for the error message i was getting
229 Views