如何在while循环[Python]中重新加载变量在后台重新加载?

时间:2018-05-21 13:40:00

标签: python python-3.x

我在后台启动一个脚本。检查脚本是否完成的唯一方法是检查日志。日志每隔几秒更新一次。

out = (os.system(comm)) #I get output of script which is running in background
outs = str(out) # convert it to string
print(type(outs.splitlines()[0])) # select only first line


while 'Job Status      : RUN OK (1)' not in outs:
    print ('Still running')
    sleep(2)
    # how I can get this 'outs' var in while loop be updated in each iteration - after 2 seconds

一般如何更新循环中的迭代项? - 就我而言,它是'outs'

1 个答案:

答案 0 :(得分:1)

你可以在睡眠后修改它,如下所示

while 'Job Status      : RUN OK (1)' not in outs:
    print ('Still running')
    sleep(2)
    outs = str(os.system(comm)))

注意:如果执行正确,os.system返回0,否则返回一些数字。您可能需要使用Subprocess中的Popen来获取cmd行命令的输出