Alex Litvinov
03/08/2024, 8:24 PMMarvin
03/08/2024, 8:24 PMAlex Litvinov
03/08/2024, 8:25 PMMarvin
03/08/2024, 8:26 PMprefect.logging
module to access Prefect's logger within your tasks and manually log the progress information captured by tqdm.
Alternatively, you may want to follow the discussion on the issue or contribute to the thread to express interest in the feature and to follow any updates on its development.Alex Litvinov
03/08/2024, 8:29 PMMarvin
03/08/2024, 8:30 PMpython
import time
import tqdm
import prefect
from prefect import task, flow
class TqdmToLogger(tqdm.tqdm):
def __init__(self, logger=None, *args, **kwargs):
super().__init__(*args, **kwargs)
self._logger = logger or prefect.context.get("logger")
def write(self, s, *args, **kwargs):
# You might want to process or filter the log message here
<http://self._logger.info|self._logger.info>(s)
@task
def my_task():
logger = prefect.context.get("logger")
for i in TqdmToLogger(range(10), logger=logger):
time.sleep(1) # Simulating work
pass # Do something
@flow
def my_flow():
my_task()
if __name__ == "__main__":
my_flow()
This custom TqdmToLogger
class inherits from tqdm
and overrides the write
method to send messages to Prefect's logger instead of the standard output. When you create a progress bar instance, you pass Prefect's logger to it. When tqdm normally prints progress updates, it will call write
, and thus in this case, those updates will go to your logs.
Keep in mind, the example above is a simplified illustration and may require adjustment based on the actual behavior of tqdm progress bars you want to capture and the specifics of Prefect's logging configuration in your environment.
The log_prints=True
decorator option in the code example provided ensures that print
statements are logged at the INFO level, which is another way to send output to logs, but this approach is not specific to tqdm.
Unfortunately, Prefect lacks a documented, built-in method for connecting tqdm directly to its logging system, and any implementation would require a custom solution like the one described above.Bring your towel and join one of the fastest growing data communities. Welcome to our second-generation open source orchestration platform, a completely rethought approach to dataflow automation.
Powered by