Python等待高达100%竞争执行下一个命令

时间:2017-06-13 05:00:18

标签: python python-2.7

以下是output1:

server                        destination-path           progress-percent
----------------- ---------------------------------      ----------------
server1            /vol/server1/vol2                      54%

输出2:

 This table is currently empty.

我需要的是我需要等待它完成100%,或者当没有输出存在时,此表当前为空。

以下是我尝试的代码,它不起作用。但我想在此处发布我在此处发布问题之前尝试的内容

def wait_to_complete(self):
    status = ''
    while not status[2] == "100%" or not status[2] == "" :
        for line in self.get_status.split("\n"): # get_status have output 1 or output 2
            if (re.search(self.vserver_name, line)) and len(line) >= 3:
                status = line.split(" ")
                status = filter(None, status)

2 个答案:

答案 0 :(得分:0)

此状态使用''进行初始化,然后访问第二个元素将产生索引错误;

尝试;

def wait_to_complete(self):
    while True:
        status = []
        for line in self.get_status.split("\n"): # get_status have output 1 or output 2
            status = filter(None, line.split(" "))
            if self.vserver_name in status: break
        if len(status) > 2 and (status[2] == '100%' or status[2] == ''):
            return

答案 1 :(得分:0)

你可以循环无限循环并在满足条件下中断

def wait_to_complete(self):
    status = ''
    while 1:
        for line in self.get_status.split("\n"):
            if (re.search(self.vserver_name, line)) and len(line) >= 3:
                status = line.split()
                if status[2] == '100%':
                    status = filter(None, status)
                    break
            elif 'This table is currently empty' in line:
                status = 'This table is currently empty'
                break