如下所示:
import threadingimport 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)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林站長站。
新聞熱點
疑難解答