Hi folks, I'm trying Prefect the first time and ha...
# prefect-community
g
Hi folks, I'm trying Prefect the first time and having a few questions regarding best practices that I didn't get a hold of in the documentation. 1. Logging: I'm extracting ~10k IDs out of a database and return that as a list of a task. The next task will map over it and continue working on them. However that list is printed in the logs completely, but I don't see value for that big chunk of log. Are there ways of disabling printing the result of a task in the logs for a single task without disabling logging in general? Or am I misusing tasks and should better return a generator instead of a list? 2. How to integrate a Prefect Flow into a CLI? I currently have a Python CLI tool that takes a database connection string (where to get the IDs from mentioned above) and processes this data with a plain loop and want to migrate that to Flow. How do I now call the flow in the CLI script in order to make the CLI script behave as a CLI should, most importantly the a exit code should be not 0 when the flow state is not successfull. Is there a guide for best practices of using prefect in CLIs?
e
First off, welcome 🙂 1. I just double checked my own flows, and the default behavior is not printing task result at all. Therefore I think you are forgetting a print/log statement somewhere in your task. We might better identify whether prefect explicitly logs the result or some task source code etc goes renegade with a
print
, if you share that section of your logs. 2. I am not exactly familiar with building CLI tools, but using
state = flow.run()
is probably the way to go. After the
run()
,
state
will be set to some state from
prefect.engine.state
.
Failed
for a failed run,
Success
for a successful run, maybe some other states for edge cases. I think that is enough to infer an exit code from.
g
Regarding 1. Actually I'm not sure which print statement was the culprit, but indeed removing all of them from the code resolved the issue. Thanks for dealing with such an obvious thing 🙈 Regarding 2. I guess that's enough I will use something like listed below. But I wondered if that might be a usecase that is used more oftenly in other peoples project and might deserve a docs page. The exit status is rather important IMO to play nicely with external task schedulers (when not using Prefects own one).
Copy code
if not state.is_successful():
  sys.exit(1)
j
@Gregor Müllegger Yeah I think if you’re use case involves exit codes you want to use a
sys.exit(0)
for successful and
sys.exit(1)
for failure (taken off of the result from the flow run). I haven’t seen this use case before and would love to hear more about it!