diff options
| author | Alexander_Kabui | 2025-01-09 10:12:02 +0300 |
|---|---|---|
| committer | Alexander_Kabui | 2025-01-09 10:12:02 +0300 |
| commit | 533bbad4cfc1101d931964321dffba4ee62d096e (patch) | |
| tree | ee36a409316b647217f1809237eb9e2d45be976f /gn3/computations/streaming.py | |
| parent | 3ecebbc7f73bffde220735a43784bd9f50b7ca1d (diff) | |
| download | genenetwork3-533bbad4cfc1101d931964321dffba4ee62d096e.tar.gz | |
feat: Add function to run an external process and capture result in a file.
Diffstat (limited to 'gn3/computations/streaming.py')
| -rw-r--r-- | gn3/computations/streaming.py | 36 |
1 files changed, 34 insertions, 2 deletions
diff --git a/gn3/computations/streaming.py b/gn3/computations/streaming.py index 7155adb..d39aa7f 100644 --- a/gn3/computations/streaming.py +++ b/gn3/computations/streaming.py @@ -1,2 +1,34 @@ -""" Module contains streaming functionality for genenetwork -""" +"""Module contains streaming procedures for genenetwork. """ +import subprocess + + +def run_process(cmd, output_file, run_id): + """Function to execute an external process and + capture the stdout in a file + input: + cmd: the command to execute as a list of args. + output_file: abs file path to write the stdout. + run_id: unique id to identify the process + + output: + Dict with the results o either success or failure. + """ + try: + # phase: execute the rscript cmd + with subprocess.Popen( + cmd, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + ) as process: + for line in iter(process.stdout.readline, b""): + # phase: capture the stdout for eaching line allowing read and write + with open(output_file, "a+", encoding="utf-8") as file_handler: + file_handler.write(line.decode("utf-8")) + process.wait() + if process.returncode == 0: + return {"msg": "success", "code": 0, "run_id": run_id} + return {"msg": "error occurred", "error": "Process failed", + "code": process.returncode, "run_id": run_id} + except subprocess.CalledProcessError as error: + return {"msg": "error occurred", + "error": str(error), "run_id": run_id} |
