#!/usr/bin/env python
# -*- coding: utf-8 -*-
import queue
import threading
class threadpool():
def __init__(self,max_num):
self.q = queue.Queue(max_num)
for i in range(max_num):
self.q.put(threading.Thread)
def get_thread(self):
return self.q.get()
def add_thread(self):
return self.q.put(threading.Thread)
pool = threadpool(5)
def func(i,pool):
import time
time.sleep(1)
print(i)
pool.add_thread()
for i in range(30):
thread = pool.get_thread()
t = thread(target=func,args=(i,pool))
t.start()
本文来自博客园,作者:孙龙-程序员,转载请注明原文链接:https://www.cnblogs.com/sunlong88/articles/8691562.html