当我可以简单地使用time.sleep()时,节流器类的用途是什么

时间:2019-01-16 09:32:28

标签: python class time delay

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)代替

0 个答案:

没有答案