当前位置 : 主页 > 编程语言 > python >

python多线程low版本

来源:互联网 收集:自由互联 发布时间:2022-06-15
#!/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 sel
#!/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()


上一篇:python中上下文管理with用法
下一篇:没有了
网友评论