class Throttle:
#add delay between download for eacch delay
def __init__(self,delay):
#amount of delay between downloads
self.delay=delay
#timestamp of when a domain was last accessed
self.domains={}
def wait(self,url):
domain=urlparse.urlparse(url).netloc
last_accessed=self.domains.get(domain)
if self.delay > 0 and last_accessed is not None:
sleeps_secs=self.delay-(datetime.datetime.now()-last_accessed).seconds
if sleeps_secs > 0:
#domain has been recently accessed
#so we need to sleep
time.sleep(sleeps_secs)
#update last accessed time
self.domains[domain]=datetime.datetime.now()
有人可以向我解释这是如何工作的,为什么这样做有用? ..我从书中得到的,
另外,为什么不使用time.sleep(2)
代替