Nicholas Andonakis
01/29/2024, 7:37 AMMarvin
01/29/2024, 7:38 AMpython
from prefect import flow, task
@task
def my_task():
return 1
@flow
def my_flow():
task_result = my_task()
return task_result + 1
result = my_flow()
assert result == 2
Here, the task my_task
returns 1
, and the flow my_flow
adds 1
to the task_result
, resulting in a successful assert
statement that the final result
is 2
.
For flows, a failure is generally indicated by an exception occurring during the flow's execution. For example:
python
from prefect import flow, task
@task
def always_fails_task():
raise ValueError("I fail successfully")
@task
def always_succeeds_task():
print("I'm fail safe!")
return "success"
@flow
def always_fails_flow():
always_fails_task.submit().result(raise_on_failure=False)
always_succeeds_task()
if __name__ == "__main__":
always_fails_flow() # This flow will ultimately fail due to the always_fails_task
In the above flow always_fails_flow
, the task always_fails_task
raises a ValueError
, which will result in the flow being marked as failed even though there is another task, always_succeeds_task
, that returns "success"
.
For a more detailed understanding or if you require specifics about the state objects and their methods, you can refer to the documents on results and flows.Nicholas Andonakis
01/29/2024, 7:39 AMMarvin
01/29/2024, 7:39 AMsubprocess.run
function in Python is used to run a command in a subshell. On success or failure, it returns a CompletedProcess
instance.
- On Success: When the command runs successfully (i.e., finishes with an exit code of 0
), the CompletedProcess
instance will have attributes like returncode
, stdout
, and stderr
. The returncode
will be 0
. If you capture the output with stdout=subprocess.PIPE
, you can access the output via the stdout
attribute.
python
import subprocess
completed = subprocess.run(['echo', 'Hello, World!'], stdout=subprocess.PIPE, text=True)
if completed.returncode == 0:
print('Command succeeded, output:', completed.stdout)
- On Failure: If the command finishes with a non-zero exit code (indicating an error), the CompletedProcess
instance's returncode
will contain that exit code. The stderr
attribute will contain the error output if stderr=subprocess.PIPE
is used.
python
import subprocess
completed = subprocess.run(['ls', 'nonexistentfile'], stderr=subprocess.PIPE, text=True)
if completed.returncode != 0:
print('Command failed with return code', completed.returncode, 'error message:', completed.stderr)
Remember, subprocess.run
does not raise an exception on a non-zero exit code by default. If you want an exception to be raised, you should pass `check=True`:
python
import subprocess
try:
subprocess.run(['ls', 'nonexistentfile'], check=True)
except subprocess.CalledProcessError as e:
print('Command failed with return code', e.returncode)
In this case, if the command fails, a CalledProcessError
exception will be raised, and you can handle it accordingly.