<@ULVA73B9P> - I have a function (not a task) that...
# ask-marvin
b
@Marvin - I have a function (not a task) that seems to be just hanging and not allowing the code to proceed. Do you have any tips for debugging this? I can't even catch where it's hanging based on the logger.info statements I have - any tips or tricks here?
m
When debugging a function that appears to be hanging in Prefect 2.0, you can use the following approaches: 1. Check for Latest Version: First, ensure you're running the latest version of Prefect, as there might have been patches or fixes since your last update that could resolve the issue. 2. Read the Logs Carefully: Logs often provide vital clues about what's going wrong. You should verify that you have carefully read through the logs to catch any messages that might indicate where and why the function is hanging. 3. Increase Logging Level: You can see more detailed logs by increasing the logging level. For instance, you can set the environment variable
PREFECT_LOGGING_LEVEL="DEBUG"
to change the default logging level for Prefect to
DEBUG
. This will generate more verbose logs, which can help identify where exactly your function is getting stuck. 4. Configure Other Libraries' Logging: If your function uses third-party libraries, you should check whether you need to modify their logging levels as well. Those libraries might have their debugging information that could reveal issues related to their use within your function. 5. Debug Incrementally: If possible, modify your function to include more
<http://logger.info|logger.info>
statements at different stages of the function's execution. This can help pinpoint exactly which part of the function is not completing. 6. Profile the Code: If your function is computationally intensive or relies on external systems/resources, consider profiling each section to determine where the bottleneck might be. 7. Check External Dependencies: If your function relies on external services, databases, or APIs, ensure that those systems are accessible and responsive as expected. 8. Isolate the Function: Run the function outside the Prefect workflow to see if it exhibits the same hanging behavior. This can help determine if the issue is with the function itself or its interaction within the Prefect ecosystem. 9. Authentication and Connection to Cloud: If your function requires access to Prefect Cloud or other secured resources, ensure that you've set up authentication correctly. Remember to revisit each part of your function to understand the paths it might be taking and validate that resources it interacts with are behaving as expected. Debugging can be a process of elimination, so be methodical in your approach.
b
i'm running prefect on AWS. is there an easy way to increase the logging level via code vs. re-deploying the instance? show me the code to do it if you can
@Marvin i'm running prefect on AWS. is there an easy way to increase the logging level via code vs. re-deploying the instance? show me the code to do it if you can
m
To increase the logging level within your Prefect code, as opposed to setting it with an environment variable during deployment, you can configure the logging programmatically in Python. Below is an example showing how to set the Prefect logger to
DEBUG
level within the code:
Copy code
python
import logging
from prefect import get_run_logger

# Configure Prefect logger to DEBUG level
prefect_logger = get_run_logger()
prefect_logger.setLevel(logging.DEBUG)

# Now you can log debug messages in your flow or tasks
prefect_logger.debug("This is a debug message")
This code configures the Prefect logger to emit
DEBUG
level logs, which are more verbose and can help you with your troubleshooting. Just be sure to include this configuration at the beginning of your flow or before the code that you are trying to debug. If you have multiple loggers or if you want to configure the root logger which affects all loggers, you can set the level as follows:
Copy code
python
# Configure root logger to DEBUG level
logging.basicConfig(level=logging.DEBUG)
It's also good practice to reset the logging level back to
INFO
or your preferred default level once you have finished debugging to avoid overwhelming your log storage with debug-level messages.
b
that was helpful 🙂
catjam 1