站长资源脚本专栏
Python 限制线程的最大数量的方法(Semaphore)
简介如下所示:import threadingimport timesem=threading.Semaphore(4) #限制线程的最大数量为4个def gothread():with sem: #锁定线程的最大数量for i in range(8):print(threading.current_
如下所示:
import threading import time sem=threading.Semaphore(4) #限制线程的最大数量为4个 def gothread(): with sem: #锁定线程的最大数量 for i in range(8): print(threading.current_thread().name,i) time.sleep(1) for i in range(5): threading.Thread(target=gothread).start()
以上这篇Python 限制线程的最大数量的方法(Semaphore)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。