From 7675612a0e7023c22b7b17b3a1ec19cce5641261 Mon Sep 17 00:00:00 2001 From: Arun Isaac Date: Thu, 24 Feb 2022 14:24:32 +0530 Subject: 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. --- gn3/computations/wgcna.py | 26 ++++++++++++-------------- 1 file 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: -- cgit v1.2.3