theading模块的Thread类
属性:
name 线程名
ident 线程标识符
daemon 布尔值,标示是否为守护线程
方法:
__init__(target=None, name=None, *args=(), **kwargs={})
start() 开始执行线程
run() 定义线程功能的方法
join(timeout=None) 阻塞线程,等待被唤醒,好于忙等待
Thread类的使用主要有三种方法:
1.创建Thread实例,传给其一个参数
2.创建Thread实例,传给其一个可调用的类实例
3.派生Thread子类,创建子类实例
下面介绍这三种用法:
1.创建Thread实例,传给其一个参数
from threading import Thread
from time import ctime, sleep loops = [4, 2] def loop(nloop, nsec):
print('loops ', nloop, 'starting at:', ctime())
sleep(nsec)
print('loop', nloop, 'end at:', ctime()) def main():
print('starting at:', ctime())
threads = [] for i in range(len(loops)):
t = Thread(target=loop, args=(i,loops[i]))
threads.append(t) ###保存类实例 for i in range(len(loops)):
threads[i].start() ###同时启动类实例 for i in range(len(loops)):
threads[i].join() ###保持主线程切出 print('all done at:', ctime()) if __name__ == '__main__':
main()
运行结果:
starting at: Mon Dec 19 23:29:58 2016
loops 0 starting at: Mon Dec 19 23:29:58 2016
loops 1 starting at: Mon Dec 19 23:29:58 2016
loop 1 end at: Mon Dec 19 23:30:00 2016
loop 0 end at: Mon Dec 19 23:30:02 2016
all done at: Mon Dec 19 23:30:02 2016
方法1,传入函数创建类实例,不用人为设置锁,释放锁
方法二:传入可调用类创建类实例(用法感觉有点像装饰器)
from threading import Thread
from time import ctime, sleep loops = [4, 2] class TFun():
def __init__(self, name, func, args):
self.name = name
self.func = func
self.args = args def __call__(self):
return self.func(*self.args) def loop(nloop, nsec):
print('loop ', nloop, 'at:', ctime())
sleep(nsec)
print('loop ', nloop, 'at:', ctime()) def main():
print('starting at:', ctime())
threads = [] for i in range(len(loops)):
t = Thread(target=TFun(loop.__name__, loop, (i,loops[i])))
threads.append(t) for i in range(len(loops)):
threads[i].start() for i in range(len(loops)):
threads[i].join() print('all done at:', ctime()) if __name__ == '__main__':
main()
运行结果:
starting at: Mon Dec 19 23:46:31 2016
loop 0 at: Mon Dec 19 23:46:31 2016
loop 1 at: Mon Dec 19 23:46:31 2016
loop 1 at: Mon Dec 19 23:46:33 2016
loop 0 at: Mon Dec 19 23:46:35 2016
all done at: Mon Dec 19 23:46:35 2016
方法三:
派生Thread子类,创建子类实例
from threading import Thread
from time import ctime, sleep loops = [4, 2] class mythread(Thread):
def __init__(self, func, args, name=''):
Thread.__init__(self)
self.name = name
self.func = func
self.args = args
self.name = name def run(self): #注意不是__call__(self)
self.func(*self.args) def loop(nloop, nsec):
print('loop', nloop, 'at:', ctime())
sleep(nsec)
print('loop ', nloop, 'end at:', ctime()) def main():
print('starting at:', ctime())
threads = [] for i in range(len(loops)):
t = mythread(loop,(i,loops[i]))
threads.append(t) for i in range(len(loops)):
threads[i].start() for i in range(len(loops)):
threads[i].join() print('end at:', ctime()) if __name__ == '__main__':
main()
运行结果:
starting at: Tue Dec 20 00:06:00 2016
loop 0 at: Tue Dec 20 00:06:00 2016
loop 1 at: Tue Dec 20 00:06:00 2016
loop 1 end at: Tue Dec 20 00:06:02 2016
loop 0 end at: Tue Dec 20 00:06:04 2016
end at: Tue Dec 20 00:06:04 2016
方法二和方法三可以测试多个函数,其中方法三更加直观。但是方法1更加简单
参考资料:Python核心编程.第四章.Wesley Chun著