簡體   English   中英

While循環with time.sleep

[英]While Loop with time.sleep

我將使用while循環刷新方法。

def usagePerUserApi():
    while True:
        url = ....
        resp = requests.get(url, headers=headers, verify=False)
        data = json.loads(resp.content)
        code = resp.status_code
        Verbindungscheck.ausgabeVerbindungsCode(code)

        head =.....

        table = []
        for item in (data['data']):
            if item['un'] == tecNo:
                table.append([
                    item['fud'],
                    item['un'],
                    str(item['lsn']),
                    str(item['fns']),
                    str(item['musage'])+"%",
                    str(item['hu']),
                    str(item['mu']),
                    str(item['hb']),
                    str(item['mb'])
                ])
        print(tabulate(table,headers=head, tablefmt="github"))
        time.sleep(300)

如果我這樣離開time.sleep,它將顯示為錯誤。 如果我將其置於while循環下,它將不斷更新,並且不會等待5分鍾。

我不知道錯誤在哪里。 我希望你能幫助我。

您需要導入python時間庫

如果你放

import time

在文件的頂部,它應該可以工作

您導入了time庫嗎? 如果不是,則添加

import time

到代碼的頂部,它應該可以工作。

還請記住,輸出緩沖可能會出現問題,導致程序無法按預期等待,因此您需要將其關閉, 如此答案所示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM