#!/usr/bin/python
#coding:utf-8
import time
# threading 提供了一个比thread模块更高层的API模块来提供线程的并发性。
# 下面表示一个程序的执行(这个例子需要5秒才能执行完成效率并不是很好)
"""
"""
"""
# 下面的例子的是使用多线程
"""
"""
#2 threading.activeCount()的使用,此方法返回当前进程中线程的个数。返回的个数中包含主线程
"""
"""
#coding:utf-8
import time
# threading 提供了一个比thread模块更高层的API模块来提供线程的并发性。
# 下面表示一个程序的执行(这个例子需要5秒才能执行完成效率并不是很好)
"""
def worker(i):
print 'worker%d'%i
time.sleep(1)
return
if __name__ == '__main__':
aa = time.time()
for x in xrange(5):
worker(x)
print time.time() - aa
# ############################
# 执行结果 :
# >>>
# worker0
# worker1
# worker2
# worker3
# worker4
# 5.04699993134
# ################################
"""
"""
# 下面的例子的是使用多线程
import threading
def worker(i):
print 'worker%d'%i
time.sleep(1)
return
if __name__ == '__main__':
aa = time.time()
for x in xrange(5):
t = threading.Thread(target=worker,args=(x,)) # target 是目标函数, args是传来的参数(用的元祖的形式进行传参)
t.start()
print time.time() - aa
#可以看到效率是很快的
# ############################
# 执行结果:
# worker0
# worker1
# worker2
# worker3
# worker4
# 0.000999927520752
# ############################
"""
"""
#2 threading.activeCount()的使用,此方法返回当前进程中线程的个数。返回的个数中包含主线程
import threading
import time
def worker(i):
print 'worker%d'%i
time.sleep(i)
return
if __name__ == '__main__':
aa = time.time()
for i in xrange(6):
t = threading.Thread(target=worker,args=(i,))
t.start()
print time.time() - aa
print u'线程总数',threading.activeCount() - 1
"""
"""
# threading.enumerate()的使用。此方法返回当前运行中的Thread对象列表
import threading
import time
def worker(i):
print 'worker%d'%i
time.sleep(1)
return
if __name__ == '__main__':
threads = []
for x in xrange(5):
t = threading.Thread(target=worker,args=(x,))
threads.append(t)
t.start()
for item in threading.enumerate():
print item
print "#################"
for v in threads:
print v
"""
"""
# threading.setDaemon()的使用。设置后台进程。
def worker(i):
time.sleep(1)
print 'worker%s'%str(i)
if __name__ == '__main__':
for i in xrange(5):
t= threading.Thread(target=worker,args=(i,))
t.setDaemon(True)
t.start()
print 'haha'
"""
import threading , time
# 继承线程函数
class MyThread(threading.Thread):
"""派生出的子线程 """
def __init__(self,func,args,name=''):
threading.Thread.__init__(self) #初始化基类
# 初始化函数
self.func = func
#初始化参数
self.args = args
#初始化名字
self.name = name
# 重新写线程的run函数
def run(self):
apply(self.func,self.args)
def threads(nloop,nsec):
print 'start loop',nloop,'at',time.ctime()
time.sleep(nsec)
print 'loop',nloop,'done at',time.ctime()
def main():
print 'starting at',time.ctime()
ths = []
nloops = range(5)
#创建线程对象
for i in nloops:
t = MyThread(threads,(i,i),threads.__name__)
ths.append(t)
# 启动线程
for i in nloops:
ths[i].start()
#等待子线程结束
for i in nloops:
ths[i].join()
print 'all Done at',time.ctime()
if __name__ == '__main__':
main()
# ############################
# >>>
# starting at Tue Dec 17 14:15:09 2013
# start loop 0 at Tue Dec 17 14:15:09 2013
# loop 0 done at Tue Dec 17 14:15:09 2013
# start loop 1 at Tue Dec 17 14:15:09 2013
# start loop 2 at Tue Dec 17 14:15:09 2013
# start loop 3 at Tue Dec 17 14:15:09 2013
# start loop 4 at Tue Dec 17 14:15:09 2013 上面标示线程同时进行了白哦是你应该开器了线程
# loop 1 done at Tue Dec 17 14:15:10 2013
# loop 2 done at Tue Dec 17 14:15:11 2013
# loop 3 done at Tue Dec 17 14:15:12 2013
# loop 4 done at Tue Dec 17 14:15:13 2013
# all Done at Tue Dec 17 14:15:13 2013
# ############################
参考地址:http://tuoxie174.blog.51cto.com/1446064/442162,
http://blog.csdn.net/cnbird2008/article/details/4997920