Cody
11/28/2023, 8:44 PMMarvin
11/28/2023, 8:44 PMMissingContextError: There is no active flow or task run context.
occurs when you try to use prefect.get_run_logger()
outside of a Prefect flow or task run context. This can happen when you're testing task functions, using concurrent futures, or in an on_cancellation hook.
Here are some related issues and their discussions:
1. prefect.get_run_logger() raises MissingContextError when testing task functions: This issue discusses the problem of getting a MissingContextError
when testing task functions. The use of prefect.get_run_logger()
within any task code will lead to a MissingContextError
if the task function is called in a test.
2. No Prefect logger available by default in a vanilla Python function, even a callable on_cancellation hook: This issue discusses the problem of getting a MissingContextError
when the on_cancellation hook is called.
3. prefect.get_run_logger() throws MissingContextError when using concurrent.futures.ThreadPoolExecutor: This issue discusses the problem of getting a MissingContextError
when prefect.get_run_logger()
is called in a method called by concurrent.futures.ThreadPoolExecutor
.
To avoid this error, ensure that you're using prefect.get_run_logger()
within a Prefect flow or task run context. If you're testing, you might need to use a different approach for logging. If you're using concurrent futures or hooks, you might need to pass the logger as an argument or find another way to handle logging.Cody
11/28/2023, 9:00 PMCody
11/28/2023, 9:02 PMMarvin
11/28/2023, 9:02 PMprefect.context
module. Here's an example:
from prefect import context
with context(run_id='test'):
logger = prefect.get_run_logger()
In this example, the run_id
is set to 'test', and then the logger is retrieved. This should prevent the MissingContextError
from being raised.
However, please note that this is a workaround and might not be the best practice. The Prefect logger is designed to be used within a flow or task run context, and using it outside of these contexts could lead to unexpected behavior. It's generally better to structure your code so that logging happens within the appropriate contexts.Nate
11/28/2023, 9:29 PM