#Semaphore 是用於控制進入數量的鎖,控制同時進行的線程,內部是基於Condition來進行實現的 #文件, 讀、寫, 寫一般只是用於一個線程寫,讀可以允許有多個 #做爬蟲 import threading import time class HtmlSpider(threading.Thread): def __init__(self, url, sem): super().__init__() self.url = url self.sem = sem def run(self): time.sleep(2) print("got html text success") self.sem.release()# release一次就會加1 class UrlProducer(threading.Thread): def __init__(self, sem): super().__init__() self.sem = sem def run(self): for i in range(20): self.sem.acquire() # acquire一次,semphore的數量就減一,知道數量為0時,它就會阻塞在這里 html_thread = HtmlSpider("https://baidu.com/{}".format(i), self.sem) html_thread.start() if __name__ == "__main__": sem = threading.Semaphore(3) url_producer = UrlProducer(sem) url_producer.start()