zoukankan      html  css  js  c++  java
  • python threading Semaphore

    #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()
  • 相关阅读:
    01--DNS服务器3
    01--DNS服务器2
    装配bean
    实现二级域名
    apache反向代理
    struts拓展restful
    restful是什么
    struts的声明式异常处理
    linux常用命令之压缩打包
    linux常用命令之文件系统
  • 原文地址:https://www.cnblogs.com/callyblog/p/11147456.html
Copyright © 2011-2022 走看看