python多线程之限制同时运行的线程个数
2017-10-02 13:47
387 查看
# -*- coding: utf-8 -*-
from __future__ import unicode_literals,division
import random,time
import threading
__author__='坏小子'
class MyThreadPool():
def __init__(self,fun,n=4):
assert n>0
self.n=n
self.i=0
self.fun=fun
self.l=[]
self.ev=threading.Event()
self.ev.set()
self.finished_ev=threading.Event()
self.finished_ev.clear()
self.i_lock=threading.Lock()
self.ev_lock=threading.Lock()
def put(self,x):
self.l.append(x)
def puts(self,l):
for x in l:
self.l.append(x)
def start(self):
if len(self.l)==0:
return
while self.l:
x=self.l.pop(0)
self.ev.wait()
t=threading.Thread(target=self.fun, args=(x,))
t_=threading.Thread(target=self.__start_a_thread, args=(t,))
t_.start()
self.i_lock.acquire()
self.i+=1
self.i_lock.release()
if self.i>=self.n:
self.ev_lock.acquire()
self.ev.clear()
self.ev_lock.release()
self.__join()
print 'thread pool finished'
def __join(self):
self.finished_ev.wait()
def __start_a_thread(self,t):
t.start()
t.join()
self.i_lock.acquire()
self.i-=1
if self.i<self.n:
self.ev_lock.acquire()
self.ev.set()
self.ev_lock.release()
self.i_lock.release()
if len(self.l)==0:#如果这是最后一个任务
self.finished_ev.set()
def test():
rd=random.random
def f(x):
print x,'start'
time.sleep(rd()*4)
print x,'finished'
l=range(10)
tp=MyThreadPool(f,5)
tp.put(l[0])
tp.puts(l)
tp.start()#然后程序会阻塞在这一句
print 'all finished !'
test()
from __future__ import unicode_literals,division
import random,time
import threading
__author__='坏小子'
class MyThreadPool():
def __init__(self,fun,n=4):
assert n>0
self.n=n
self.i=0
self.fun=fun
self.l=[]
self.ev=threading.Event()
self.ev.set()
self.finished_ev=threading.Event()
self.finished_ev.clear()
self.i_lock=threading.Lock()
self.ev_lock=threading.Lock()
def put(self,x):
self.l.append(x)
def puts(self,l):
for x in l:
self.l.append(x)
def start(self):
if len(self.l)==0:
return
while self.l:
x=self.l.pop(0)
self.ev.wait()
t=threading.Thread(target=self.fun, args=(x,))
t_=threading.Thread(target=self.__start_a_thread, args=(t,))
t_.start()
self.i_lock.acquire()
self.i+=1
self.i_lock.release()
if self.i>=self.n:
self.ev_lock.acquire()
self.ev.clear()
self.ev_lock.release()
self.__join()
print 'thread pool finished'
def __join(self):
self.finished_ev.wait()
def __start_a_thread(self,t):
t.start()
t.join()
self.i_lock.acquire()
self.i-=1
if self.i<self.n:
self.ev_lock.acquire()
self.ev.set()
self.ev_lock.release()
self.i_lock.release()
if len(self.l)==0:#如果这是最后一个任务
self.finished_ev.set()
def test():
rd=random.random
def f(x):
print x,'start'
time.sleep(rd()*4)
print x,'finished'
l=range(10)
tp=MyThreadPool(f,5)
tp.put(l[0])
tp.puts(l)
tp.start()#然后程序会阻塞在这一句
print 'all finished !'
test()
相关文章推荐
- 多线程学习之限制同时运行的线程数量
- 多线程学习之限制同时运行的线程数量
- Python开启多线程,并同时等待每个线程完成后再推出
- C++ windows多线程 线程描述了进程内代码的执行路径。进程中同时可以有多个线程在执行,为了使他们能够同时运行,操作系统为每个线程轮流分配CPU时间片,为了充分地利用CPU提高软件产品的性能,一
- JAVA基础学习--一个线程加一运算,一个线程做减一运算,多个线程同时交替运行--synchronized
- JAVA多线程之两个线程同时写一个文件
- Python多线程运行带多个参数的函数
- 分享:python,限制任意函数,线程的执行时间或根据条件终止.
- 多线程Demo-Semaphore 控制同时访问资源的线程个数
- (C#) 线程操作 - 限制可同时访问某一资源或资源池的线程数。Semaphore 类。Mutex类
- python 多线程中子线程和主线程相互通信
- python 单线程和多线程
- iOS多线程的初步研究(五)-- 如何让NSURLConnection在子线程中运行
- 2.使用synchronized关键字实现多线程的同步和互斥(不同线程同时读写同一数据)
- iOS多线程的初步研究(五)-- 如何让NSURLConnection在子线程中运行
- iOS 多线程初步研究-NSURLConnection在子线程中运行
- 同名程序同时运行限制
- 提供一个“多线程(或进程)同时退出时判断是哪些线程退出”的方法
- vs2008 在多线程环境下,有没有调试单个线程运行的功能?
- iOS多线程的初步研究(五)-- 如何让NSURLConnection在子线程中运行