0

I have the following for loop:

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)

To avoid API limitations, I want to pause the loop for 600 seconds each time 100 direct messages are sent.

What's the best way to do it?

munich
  • 500
  • 1
  • 6
  • 16

4 Answers4

3

You can try this:

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
Hackaholic
  • 19,069
  • 5
  • 54
  • 72
  • 2
    It should probably be `== 99`, though. Also, `time.sleep` takes seconds, not milliseconds. – Ry- Jan 04 '15 at 20:16
  • 1
    `i+1 % 100` won't do what you want in most languages. Is Python different here, or do you need parens to override the precedence of `%`? – cHao Jan 04 '15 at 20:18
  • yeaaa thanks for notification, i missed that – Hackaholic Jan 04 '15 at 20:20
1

You could do this:

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)
duhaime
  • 25,611
  • 17
  • 169
  • 224
0
  1. Use a counter within the loop, counting up to 100 messages.
  2. When counter == 100, use

    from time import sleep
    sleep(AMOUNT_OF_TIME)
    
Jobs
  • 3,317
  • 6
  • 26
  • 52
0

You can use:

import time

and then

time.sleep(seconds)

to sleep. You can use floats for smaller time than a second.

One thing to note, people will likely hate you if you do this.

MightyPork
  • 18,270
  • 10
  • 79
  • 133