首页 > 解决方案 > 基于python中的计数的计时器

问题描述

我正在尝试将我的查询值传递给外部 API,但由于 API 存在限制问题,我每 5 分钟只能进行 500 次调用,并且需要 10 分钟的冷却时间。所以我正在尝试,我添加了一个计数增量方法,如果计数超过 400 则休眠 2 分钟,我知道错误在我的代码中,但我不知道如何编写函数。

for info in contact:
    id = info[0]
    name = info[1]

    print(id)
    count += 1
    print(count)

    if count > 400:
        print('11111111')
        time.sleep(120)
        print ("count crossed 400....")
        print ("sleep for 2 minutes")
    else:
        logging.info(count)
        print(name) 
        try:
            ...

所以这将检查计数,如果计数超过 400,它将为每个其他呼叫休眠 2 分钟,直到完成。我试图以这样一种方式做到这一点,即每 400 个调用睡眠 2 分钟,如何在 python 中编写它。好心指导

标签: python

解决方案


for info in contact:
        id = info[0]
        name = info[1] 
        print(id)
        count += 1
        print(count)
        if count > 400:
            print('11111111')
            time.sleep(120)
            count = 0
            print ("count crossed 400....")
            print ("sleep for 2 minutes")
        else:
            logging.info(count)
            print(name)

添加行 : count = 0,它将变量重置count为 0if count > 400


推荐阅读