当我们需要执行外部命令或自己写一个自动化执行器时,需要使用到启动进程并获取输出的操作
首先,我们启动进程采用Python的subprocess模块,为了保证标准输出和标准错误输出能够正常运行,启动两个线程来检测输出结果部分
class Daemon(threading.Thread):
def __init__(self, workDir, logFunction=None, *args):
threading.Thread.__init__(self)
self.process = None
self.args = args
self.workDir = workDir
self.errList = []
self.stdOut = ""
self.stdErr = ""
self.isEnd = False
self.logFunction = logFunction
def _start_process(self):
if self.workDir == "":
self.workDir = None
allParas = []
for arg in self.args:
if arg:
allParas.extend(arg)
commandStr = " ".join(allParas)
try:
commandStr = commandStr.encode("gbk")
except:
pass