about summary refs log tree commit diff
path: root/gn3/computations
diff options
context:
space:
mode:
authorArun Isaac2022-02-24 14:24:32 +0530
committerArun Isaac2022-02-24 14:24:32 +0530
commit7675612a0e7023c22b7b17b3a1ec19cce5641261 (patch)
tree257cbe868cc977867ea1af557d9e5167dee128bb /gn3/computations
parentaaff8b8ac968bce9821d6fef22b1296247a9df09 (diff)
downloadgenenetwork3-7675612a0e7023c22b7b17b3a1ec19cce5641261.tar.gz
gn3: computations: Call Popen with context manager.
Context managers should be preferred when allocating resources.

* gn3/computations/wgcna.py (stream_cmd_output): Call Popen with context
manager.
Diffstat (limited to 'gn3/computations')
-rw-r--r--gn3/computations/wgcna.py26
1 files changed, 12 insertions, 14 deletions
diff --git a/gn3/computations/wgcna.py b/gn3/computations/wgcna.py
index de26f48..c985491 100644
--- a/gn3/computations/wgcna.py
+++ b/gn3/computations/wgcna.py
@@ -31,20 +31,18 @@ def stream_cmd_output(socketio, request_data, cmd: str):
 
     socketio.emit("output", {"data": f"calling you script {cmd}"},
                   namespace="/", room=request_data["socket_id"])
-    results = subprocess.Popen(
-        cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
-
-    if results.stdout is not None:
-
-        for line in iter(results.stdout.readline, b""):
-            socketio.emit("output",
-                          {"data": line.decode("utf-8").rstrip()},
-                          namespace="/", room=request_data["socket_id"])
-
-        socketio.emit(
-            "output", {"data":
-                       "parsing the output results"}, namespace="/",
-            room=request_data["socket_id"])
+    with subprocess.Popen(
+            cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True) as results:
+        if results.stdout is not None:
+            for line in iter(results.stdout.readline, b""):
+                socketio.emit("output",
+                              {"data": line.decode("utf-8").rstrip()},
+                              namespace="/", room=request_data["socket_id"])
+
+                socketio.emit(
+                    "output", {"data":
+                               "parsing the output results"}, namespace="/",
+                    room=request_data["socket_id"])
 
 
 def process_image(image_loc: str) -> bytes: