https://prefect.io logo
#prefect-community
Title
# prefect-community
p

Pedro Henrique

10/14/2022, 4:41 PM
after adding call my functions the following error presented when execute File "/home/cpfl0dev/.local/lib/python3.8/site-packages/prefect/logging/loggers.py", line 110, in get_run_logger raise MissingContextError("There is no active flow or task run context.") prefect.exceptions.MissingContextError: There is no active flow or task run context.
a

Andrew Brookins

10/14/2022, 4:50 PM
Hi, Pedro! Can you give us example code that reproduces the error, and paste the full stack trace of the error?
p

Pedro Henrique

10/14/2022, 4:58 PM
yes this is traceback when i dont add .fn() in the end of the tasks
165811.204 | ERROR | Flow run 'independent-mamba' - Finished in state Failed('Flow run encountered an exception. RuntimeError: Tasks cannot be run outside of a flow. To call the underlying task function outside of a flow use
task.fn()
.\n') Traceback (most recent call last): File "/usr/lib/python3.8/runpy.py", line 194, in _run_module_as_main return _run_code(code, main_globals, None, File "/usr/lib/python3.8/runpy.py", line 87, in _run_code exec(code, run_globals) File "/home/cpfl0dev/.vscode-server/extensions/ms-python.python-2022.16.1/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher/../../debugpy/__main__.py", line 39, in <module> cli.main() File "/home/cpfl0dev/.vscode-server/extensions/ms-python.python-2022.16.1/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 430, in main run() File "/home/cpfl0dev/.vscode-server/extensions/ms-python.python-2022.16.1/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 284, in run_file runpy.run_path(target, run_name="__main__") File "/home/cpfl0dev/.vscode-server/extensions/ms-python.python-2022.16.1/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 321, in run_path return _run_module_code(code, init_globals, run_name, File "/home/cpfl0dev/.vscode-server/extensions/ms-python.python-2022.16.1/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 135, in _run_module_code _run_code(code, mod_globals, init_globals, File "/home/cpfl0dev/.vscode-server/extensions/ms-python.python-2022.16.1/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 124, in _run_code exec(code, run_globals) File "/home/cpfl0dev/de3075/flows_prod/pipeline_fire.py", line 221, in <module> flow_function() File "/home/cpfl0dev/.local/lib/python3.8/site-packages/prefect/flows.py", line 429, in call return enter_flow_run_engine_from_flow_call( File "/home/cpfl0dev/.local/lib/python3.8/site-packages/prefect/engine.py", line 150, in enter_flow_run_engine_from_flow_call return anyio.run(begin_run) File "/home/cpfl0dev/.local/lib/python3.8/site-packages/anyio/_core/_eventloop.py", line 56, in run return asynclib.run(func, *args, **backend_options) # type: ignore File "/home/cpfl0dev/.local/lib/python3.8/site-packages/anyio/_backends/_asyncio.py", line 230, in run return native_run(wrapper(), debug=debug) File "/usr/lib/python3.8/asyncio/runners.py", line 44, in run return loop.run_until_complete(main) File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete return future.result() File "/home/cpfl0dev/.local/lib/python3.8/site-packages/anyio/_backends/_asyncio.py", line 225, in wrapper return await func(*args) File "/home/cpfl0dev/.local/lib/python3.8/site-packages/prefect/client/utilities.py", line 47, in with_injected_client return await fn(*args, **kwargs) File "/home/cpfl0dev/.local/lib/python3.8/site-packages/prefect/engine.py", line 229, in create_then_begin_flow_run return await state.result(fetch=True) File "/home/cpfl0dev/.local/lib/python3.8/site-packages/prefect/states.py", line 74, in _get_state_result raise await get_state_exception(state) File "/home/cpfl0dev/.local/lib/python3.8/site-packages/prefect/engine.py", line 578, in orchestrate_flow_run result = await run_sync(flow_call) File "/home/cpfl0dev/.local/lib/python3.8/site-packages/prefect/utilities/asyncutils.py", line 68, in run_sync_in_worker_thread return await anyio.to_thread.run_sync(call, cancellable=True) File "/home/cpfl0dev/.local/lib/python3.8/site-packages/anyio/to_thread.py", line 28, in run_sync return await get_asynclib().run_sync_in_worker_thread(func, *args, cancellable=cancellable, File "/home/cpfl0dev/.local/lib/python3.8/site-packages/anyio/_backends/_asyncio.py", line 802, in run_sync_in_worker_thread return await future File "/home/cpfl0dev/.local/lib/python3.8/site-packages/anyio/_backends/_asyncio.py", line 740, in run result = func(*args) File "/home/cpfl0dev/de3075/flows_prod/pipeline_fire.py", line 178, in flow_function datetime_now = get_datetimenow() File "/home/cpfl0dev/.local/lib/python3.8/site-packages/prefect/tasks.py", line 330, in call return enter_task_run_engine( File "/home/cpfl0dev/.local/lib/python3.8/site-packages/prefect/engine.py", line 672, in enter_task_run_engine raise RuntimeError( RuntimeError: Tasks cannot be run outside of a flow. To call the underlying task function outside of a flow use
task.fn()
.
after i addeded .fn() for my tasks the following error is presented
170014.182 | ERROR | Flow run 'roaring-worm' - Finished in state Failed('Flow run encountered an exception. MissingContextError: There is no active flow or task run context.\n') Traceback (most recent call last): File "/usr/lib/python3.8/runpy.py", line 194, in _run_module_as_main return _run_code(code, main_globals, None, File "/usr/lib/python3.8/runpy.py", line 87, in _run_code exec(code, run_globals) File "/home/cpfl0dev/.vscode-server/extensions/ms-python.python-2022.16.1/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher/../../debugpy/__main__.py", line 39, in <module> cli.main() File "/home/cpfl0dev/.vscode-server/extensions/ms-python.python-2022.16.1/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 430, in main run() File "/home/cpfl0dev/.vscode-server/extensions/ms-python.python-2022.16.1/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 284, in run_file runpy.run_path(target, run_name="__main__") File "/home/cpfl0dev/.vscode-server/extensions/ms-python.python-2022.16.1/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 321, in run_path return _run_module_code(code, init_globals, run_name, File "/home/cpfl0dev/.vscode-server/extensions/ms-python.python-2022.16.1/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 135, in _run_module_code _run_code(code, mod_globals, init_globals, File "/home/cpfl0dev/.vscode-server/extensions/ms-python.python-2022.16.1/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 124, in _run_code exec(code, run_globals) File "/home/cpfl0dev/de3075/flows_prod/pipeline_fire.py", line 221, in <module> flow_function() File "/home/cpfl0dev/.local/lib/python3.8/site-packages/prefect/flows.py", line 429, in call return enter_flow_run_engine_from_flow_call( File "/home/cpfl0dev/.local/lib/python3.8/site-packages/prefect/engine.py", line 150, in enter_flow_run_engine_from_flow_call return anyio.run(begin_run) File "/home/cpfl0dev/.local/lib/python3.8/site-packages/anyio/_core/_eventloop.py", line 56, in run return asynclib.run(func, *args, **backend_options) # type: ignore File "/home/cpfl0dev/.local/lib/python3.8/site-packages/anyio/_backends/_asyncio.py", line 230, in run return native_run(wrapper(), debug=debug) File "/usr/lib/python3.8/asyncio/runners.py", line 44, in run return loop.run_until_complete(main) File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete return future.result() File "/home/cpfl0dev/.local/lib/python3.8/site-packages/anyio/_backends/_asyncio.py", line 225, in wrapper return await func(*args) File "/home/cpfl0dev/.local/lib/python3.8/site-packages/prefect/client/utilities.py", line 47, in with_injected_client return await fn(*args, **kwargs) File "/home/cpfl0dev/.local/lib/python3.8/site-packages/prefect/engine.py", line 229, in create_then_begin_flow_run return await state.result(fetch=True) File "/home/cpfl0dev/.local/lib/python3.8/site-packages/prefect/states.py", line 74, in _get_state_result raise await get_state_exception(state) File "/home/cpfl0dev/.local/lib/python3.8/site-packages/prefect/engine.py", line 578, in orchestrate_flow_run result = await run_sync(flow_call) File "/home/cpfl0dev/.local/lib/python3.8/site-packages/prefect/utilities/asyncutils.py", line 68, in run_sync_in_worker_thread return await anyio.to_thread.run_sync(call, cancellable=True) File "/home/cpfl0dev/.local/lib/python3.8/site-packages/anyio/to_thread.py", line 28, in run_sync return await get_asynclib().run_sync_in_worker_thread(func, *args, cancellable=cancellable, File "/home/cpfl0dev/.local/lib/python3.8/site-packages/anyio/_backends/_asyncio.py", line 802, in run_sync_in_worker_thread return await future File "/home/cpfl0dev/.local/lib/python3.8/site-packages/anyio/_backends/_asyncio.py", line 740, in run result = func(*args) File "/home/cpfl0dev/de3075/flows_prod/pipeline_fire.py", line 178, in flow_function datetime_now = get_datetimenow.fn() File "/home/cpfl0dev/de3075/flows_prod/pipeline_fire.py", line 43, in get_datetimenow logger_prefect = get_run_logger() File "/home/cpfl0dev/.local/lib/python3.8/site-packages/prefect/logging/loggers.py", line 110, in get_run_logger raise MissingContextError("There is no active flow or task run context.") prefect.exceptions.MissingContextError: There is no active flow or task run context.
s

Serina

10/20/2022, 8:58 PM
Hi @Pedro Henrique please check out this other thread: https://prefect-community.slack.com/archives/CL09KU1K7/p1664374981868889 Or you could probably just add this after your imports in order to run tasks outside your flow (while testing only)
Copy code
import logging
def get_run_logger():
    return logging.getLogger("my_logger")
3 Views