Python将等待100%的时间来执行下一个命令

2024-04-26 01:26:45 发布

您现在位置:Python中文网/ 问答频道 /正文

以下是输出1:

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)

Tags: orpathselfoutputgetserverstatusline
2条回答

这里的状态是用''初始化的,然后访问第二个元素将给出索引错误

尝试

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

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

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

相关问题 更多 >