Python For循环:在100次循环后如何暂停x秒?

Python For循环:在100次循环后如何暂停x秒?,python,Python,对于循环,我有以下: for user_id in new_followers: try: t.direct_messages.new(user_id=user_id, text="Thanks for following. Etc etc etc") print("Sent DM to %d" % (user_id)) followers_old.add(user_id) 为了避免API限制,每次发送100条直接消息时,我希望将

对于循环,我有以下

for user_id in new_followers:
    try:    
        t.direct_messages.new(user_id=user_id, text="Thanks for following. Etc etc etc")
        print("Sent DM to %d" % (user_id))
        followers_old.add(user_id)
为了避免API限制,每次发送100条直接消息时,我希望将循环暂停600秒

最好的方法是什么

  • 在循环中使用
    计数器
    ,最多可计算100条消息
  • 计数器==100
    时,使用

    from time import sleep
    sleep(AMOUNT_OF_TIME)
    
  • 在循环中使用
    计数器
    ,最多可计算100条消息
  • 计数器==100
    时,使用

    from time import sleep
    sleep(AMOUNT_OF_TIME)
    
  • 您可以使用:

    import time
    
    然后

    time.sleep(seconds)
    
    睡觉。您可以使用浮动的时间小于一秒钟

    需要注意的是,如果你这样做,人们可能会恨你。

    你可以使用:

    import time
    
    然后

    time.sleep(seconds)
    
    睡觉。您可以使用浮动的时间小于一秒钟

    需要注意的是,如果你这样做,人们可能会恨你。

    你可以这样做:

    import time
    
    count = 0
    
    for user_id in new_followers:
        count +=1 
        if count == 100:
            count = 0
            time.sleep(600)
        try:    
            t.direct_messages.new(user_id=user_id, text="Thanks for following. Etc etc etc")
            print("Sent DM to %d" % (user_id))
            followers_old.add(user_id)
    
    您可以这样做:

    import time
    
    count = 0
    
    for user_id in new_followers:
        count +=1 
        if count == 100:
            count = 0
            time.sleep(600)
        try:    
            t.direct_messages.new(user_id=user_id, text="Thanks for following. Etc etc etc")
            print("Sent DM to %d" % (user_id))
            followers_old.add(user_id)
    
    您可以尝试以下方法:

    for i, user_id in enumerate(new_followers):
        try:    
            t.direct_messages.new(user_id=user_id, text="Thanks for following. Etc etc etc")
            print("Sent DM to %d" % (user_id))
            followers_old.add(user_id)
            if (i+1)%100 == 0:
                 time.sleep(x) # here x in milisecond
    
    您可以尝试以下方法:

    for i, user_id in enumerate(new_followers):
        try:    
            t.direct_messages.new(user_id=user_id, text="Thanks for following. Etc etc etc")
            print("Sent DM to %d" % (user_id))
            followers_old.add(user_id)
            if (i+1)%100 == 0:
                 time.sleep(x) # here x in milisecond
    

    @不太可能。我知道如何休眠python程序,但我的问题是在完成X循环后休眠它。你可以使用time.sleep(X),这里X是用mili表示的sec@vaultah不是真的。我知道如何使python程序休眠,但我的问题是在X循环完成后休眠。你可以使用时间。休眠(X),这里的X以毫秒为单位。我的问题是在X循环完成后休眠。哦,使用变量进行计数??我的问题是在X循环完成后休眠。哦,使用一个变量进行计数??它可能应该是
    ==99
    。另外,
    time.sleep
    需要几秒钟,而不是几毫秒。
    i+1%100
    在大多数语言中都做不到您想要的。Python在这里是不同的,还是需要参数来覆盖
    %
    的优先级?是的,谢谢你的通知,不过我错过了它应该是
    ==99
    。另外,
    time.sleep
    需要几秒钟,而不是几毫秒。
    i+1%100
    在大多数语言中都做不到您想要的。Python在这里是不同的,还是需要parens来覆盖
    %
    的优先级?是的,谢谢你的通知,我错过了那个人,在这个问题上我们就像鲨鱼一样亲密!伙计,在这个问题上我们就像鲨鱼一样亲密!