update
This commit is contained in:
@@ -1,48 +0,0 @@
|
||||
import os, sys, traceback, subprocess, json, platform
|
||||
from . import logger
|
||||
|
||||
|
||||
class SupportProcess(object):
|
||||
|
||||
@classmethod
|
||||
def execute(cls, command, format=None, shell=False, env=None, timeout=1000):
|
||||
logger.debug(command)
|
||||
try:
|
||||
if platform.system() == 'Windows':
|
||||
command = ' '.join(command)
|
||||
|
||||
iter_arg = ''
|
||||
process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, shell=shell, env=env, encoding='utf8')
|
||||
try:
|
||||
process_ret = process.wait(timeout=timeout) # wait for the subprocess to exit
|
||||
except:
|
||||
import psutil
|
||||
process = psutil.Process(process.pid)
|
||||
for proc in process.children(recursive=True):
|
||||
proc.kill()
|
||||
process.kill()
|
||||
return "timeout"
|
||||
ret = []
|
||||
with process.stdout:
|
||||
for line in iter(process.stdout.readline, iter_arg):
|
||||
ret.append(line.strip())
|
||||
|
||||
if format is None:
|
||||
ret2 = '\n'.join(ret)
|
||||
elif format == 'json':
|
||||
try:
|
||||
index = 0
|
||||
for idx, tmp in enumerate(ret):
|
||||
#logger.debug(tmp)
|
||||
if tmp.startswith('{') or tmp.startswith('['):
|
||||
index = idx
|
||||
break
|
||||
ret2 = json.loads(''.join(ret[index:]))
|
||||
except:
|
||||
ret2 = None
|
||||
return ret2
|
||||
except Exception as e:
|
||||
logger.error(f'Exception:{str(e)}', )
|
||||
logger.error(traceback.format_exc())
|
||||
logger.error('command : %s', command)
|
||||
|
||||
@@ -132,13 +132,16 @@ class SupportSubprocess(object):
|
||||
self.start_send_callback()
|
||||
if self.process is not None:
|
||||
self.process.wait()
|
||||
#logger.info(f"{self.command} 정상 종료")
|
||||
if self.stdout_queue != None:
|
||||
self.stdout_queue.put('<END>')
|
||||
logger.info(f"{self.command} 정상 종료")
|
||||
except Exception as e:
|
||||
logger.error(f'Exception:{str(e)}')
|
||||
logger.error(traceback.format_exc())
|
||||
|
||||
if self.stdout_callback != None:
|
||||
self.stdout_callback('error', str(e))
|
||||
self.stdout_callback('error', str(traceback.format_exc()))
|
||||
finally:
|
||||
if self.stdout_callback != None:
|
||||
self.stdout_callback('thread_end', None)
|
||||
|
||||
|
||||
def start_communicate(self):
|
||||
@@ -150,8 +153,6 @@ class SupportSubprocess(object):
|
||||
|
||||
def rdr():
|
||||
while True:
|
||||
if self.process == None:
|
||||
break
|
||||
buf = self.process.stdout.read(1)
|
||||
if buf:
|
||||
_queue.put( buf )
|
||||
@@ -178,10 +179,9 @@ class SupportSubprocess(object):
|
||||
except:
|
||||
pass
|
||||
if r is not None:
|
||||
if self.stdout_queue != None:
|
||||
self.stdout_queue.put(r)
|
||||
if self.stdout_queue != None: # 사용자 중지
|
||||
self.stdout_queue.put('<END>')
|
||||
#print(f"{r=}")
|
||||
self.stdout_queue.put(r)
|
||||
self.stdout_queue.put('<END>')
|
||||
for tgt in [rdr, clct]:
|
||||
th = threading.Thread(target=tgt)
|
||||
th.setDaemon(True)
|
||||
@@ -200,10 +200,6 @@ class SupportSubprocess(object):
|
||||
else:
|
||||
if self.stdout_callback != None:
|
||||
self.stdout_callback('log', line)
|
||||
self.send_to_ui_thread = None
|
||||
self.stdout_queue = None
|
||||
self.process = None
|
||||
|
||||
th = threading.Thread(target=func, args=())
|
||||
th.setDaemon(True)
|
||||
th.start()
|
||||
@@ -222,7 +218,7 @@ class SupportSubprocess(object):
|
||||
logger.error(traceback.format_exc())
|
||||
finally:
|
||||
try:
|
||||
self.stdout_queue = None
|
||||
#self.stdout_queue = None
|
||||
self.process.kill()
|
||||
except: pass
|
||||
|
||||
|
||||
Reference in New Issue
Block a user