2015-12-15 122 views
0

我写了这个函数来处理Tweepy光标的“速率限制错误”,以便继续从Twitter API下载。把几个线程放在睡眠/等待不使用Time.Sleep()

def limit_handled(cursor, user): 
    over = False 
    while True: 
     try: 
      if (over == True): 
       print "Routine Riattivata, Serviamo il numero:", user 
       over = False 
      yield cursor.next() 
     except tweepy.RateLimitError: 
      print "Raggiunto Limite, Routine in Pausa" 
      threading.Event.wait(15*60 + 15) 
      over = True 
     except tweepy.TweepError: 
      print "TweepError" 
      threading.Event.wait(5) 

由于我使用serveral的螺纹连接,我想阻止他们的每一个当RateLimitError错误引发,15分钟后重新启动。 我以前使用的功能:

time.sleep(x) 

但我明白,不能对线程工作得很好(如果线程未激活计数器不增加),所以我试图用:

threading.Event.wait(x) 

但这个错误提出:

Exception in thread Thread-15: 
Traceback (most recent call last): 
    File "/home/xor/anaconda/lib/python2.7/threading.py", line 810, in __bootstrap_inner 
    self.run() 
    File "/home/xor/anaconda/lib/python2.7/threading.py", line 763, in run 
    self.__target(*self.__args, **self.__kwargs) 
    File "/home/xor/spyder/algo/HW2/hw2.py", line 117, in work 
    storeFollowersOnMDB(ids, api, k) 
    File "/home/xor/spyder/algo/HW2/hw2.py", line 111, in storeFollowersOnMDB 
    for followersPag in limit_handled(tweepy.Cursor(api.followers_ids, id = user, count=5000).pages(), user): 
    File "/home/xor/spyder/algo/HW2/hw2.py", line 52, in limit_handled 
    threading.Event.wait(15*60 + 15) 
AttributeError: 'function' object has no attribute 'wait' 

我怎样才能“睡眠/等待”我的螺纹为确保他们会在适当的时候醒来?

回答

2

尝试做这样的代替:

import threading 
dummy_event = threading.Event() 
dummy_event.wait(timeout=1) 

也尝试谷歌,荷兰国际集团第一下一次:Issues with time.sleep and Multithreading in Python

+0

非常感谢你,我用Google搜索,但没有成功。对不起浪费时间! – Aalto

+0

啊没问题,看到你刚刚开始,所以我不会downvote。我也希望你会喜欢这个社区c: – ivan