将几个线程放在 sleep/wait 中而不使用 Time.Sleep()
Put several Threads in sleep/wait not using Time.Sleep()
我编写了这个处理 "rate limit error" Tweepy 光标的函数,以便继续从 Twitter API 下载。
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)
因为我使用多个线程进行连接,所以我想在出现 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'
我怎样才能 "sleep/wait" 我的线程确保它们会在正确的时刻醒来?
尝试这样做:
import threading
dummy_event = threading.Event()
dummy_event.wait(timeout=1)
下次也先尝试 google-ing:Issues with time.sleep and Multithreading in Python
我编写了这个处理 "rate limit error" Tweepy 光标的函数,以便继续从 Twitter API 下载。
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)
因为我使用多个线程进行连接,所以我想在出现 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'
我怎样才能 "sleep/wait" 我的线程确保它们会在正确的时刻醒来?
尝试这样做:
import threading
dummy_event = threading.Event()
dummy_event.wait(timeout=1)
下次也先尝试 google-ing:Issues with time.sleep and Multithreading in Python