python 爬虫多线程

https://www.zhihu.com/question/263977050

 

https://blog.csdn.net/kaikai136412162/article/details/100678339

#encoding=utf-8
import time
import threading
start = time.perf_counter()

def do_something(num):
    lock.acquire()
    print(f"-> 线程启动{num}")
    lock.release()
    print(f"-> 线程结束{num}")
    pool.release()

if __name__=="__main__":
    lock = threading.Lock()
    max_thread=3
    pool = threading.BoundedSemaphore(max_thread)
    thread_list = []
    for i in range(1,100):
        pool.acquire()
        thread = threading.Thread(target=do_something,args=[i])
        thread.start()
        thread_list.append(thread)

    for j in thread_list:
        j.join()

    finish = time.perf_counter()
    print(f"全部任务执行完成,耗时 {round(finish - start,2)} 秒")

  

posted @ 2022-08-19 14:58  星云惊蛰  阅读(27)  评论(0)    收藏  举报