网络编程
位置:首页>> 网络编程>> Python编程>> Python 限制线程的最大数量的方法(Semaphore)

Python 限制线程的最大数量的方法(Semaphore)

作者:houyanhua1  发布时间:2022-03-02 06:24:09 

标签:Python,线程,数量,Semaphore

如下所示:


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()

来源:https://blog.csdn.net/houyanhua1/article/details/78229034

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com