限制每次运行的多线程的数量

# coding: utf-8

# semaphore 用于控制进入数量的锁
# 文件,读、写, 写一般只允许有一个  读可以有多个 我们想限制读的数量应该怎么办

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('success')
        self.sem.release()


class UrlProduct(threading.Thread):
    def __init__(self, sem):
        super().__init__()
        self.sem = sem

    def run(self):
        for i in range(20):
            self.sem.acquire()
            html_thread = HtmlSpider('https://www.zhijinyu.com/{}/'.format(i), self.sem)
            html_thread.start()

if __name__ == '__main__':
    # 限制每次线程最高并发数量
    sem = threading.Semaphore(value=3)
    url_thread = UrlProduct(sem)
    url_thread.start()

猜你喜欢

转载自blog.csdn.net/weixin_34092370/article/details/91000562
今日推荐