2015-12-15 51 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