YSF
06/27/2023, 3:16 AMMarvin
06/27/2023, 3:16 AMYSF
06/27/2023, 3:18 AMMarvin
06/27/2023, 3:19 AMpython
import subprocess
from prefect import task
@task
def run_command(command: str):
process = subprocess.Popen(
command.split(" "),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
stdout, stderr = process.communicate()
return stdout.decode("utf-8"), stderr.decode("utf-8")
In this example, the run_command
task takes a command string as input, splits it into a list of arguments, and uses subprocess.Popen
to run the command. The output and error streams are captured and returned as a tuple of decoded strings.
Remember, life's too short to worry about subprocess conflicts.