python基础===trheading 模块

'''threading模块'''

import threading
import time

def music(func):
    for i in range(2):
        print("[+]i was listening to  %s!  %s" %(func,time.ctime()))
        time.sleep(3)

def movie(func):
    for i in range(2):
        print("[+]i was watching the movie %s! %s" %(func,time.ctime()))
        time.sleep(2)

#创建线程数组
threads=[]

#创建线程t1,加入到线程数组
t1 = threading.Thread(target = music, args = (u"云中谁在歌",))
threads.append(t1)

#创建线程t2,加入到线程数组
t2 = threading.Thread(target = movie, args = (u"速度与激情",))
threads.append(t2)

if __name__ ==  '__main__':
    #启动线程
    for i in threads:
        i.start()
    #守护线程
    for i in threads:
        i.join()
    print("[+]end time: %s" %(time.ctime()))

 Python 通过两个标准库thread 和threading 提供对线程的支持。thread 提供了低级别的、原始的线程以及一个简单的锁。threading 基于Java 的线程模型设计。锁(Lock)和条件变量(Condition)在Java中是对象的基本行为(每一个对象都自带了锁和条件变量),而在Python 中则是独立的对象。

我们应该避免使用thread 模块,原因是它不支持守护线程。当主线程退出时,所有的子线程不论它们是否还在工作,都会被强行退出。有时我们并不期望这种行为,这时就引入了守护线程的概念。threading模块则支持守护线程。

  • start() 开始线程活动。
  • join() 等待线程终止。

通过for 循环遍历thread 数组中所装载的线程;然后通过start()函数启动每一个线程。
join()会等到线程结束,或者在给了timeout 参数的时候,等到超时为止。join()的另一个比较重要的方面是它可以完全不用调用。一旦线程启动后,就会一直运行,直到线程的函数结束,退出为止。

 

当然,从上面例子中发现线程的创建是颇为麻烦的,每创建一个线程都需要创建一个t(t1、t2、...),如果创建的线程较多时这样极其不方便。下面对通过例子进行改进:

 

from time import sleep, ctime
import threading

def muisc(func):   for i in range(2):     print('Start playing: %s! %s' %(func,ctime()))     sleep(2) def move(func):   for i in range(2):     print ('Start playing: %s! %s' %(func,ctime()))     sleep(5)

#判断文件类型,交给相应的函数执行
def player(name):
  r = name.split('.')[1]
  if r == 'mp3':
    muisc(name)
  elif r == 'mp4':
    move(name)
  else:
    print ('error: The format is not recognized!')

list = ['爱情买卖.mp3','阿凡达.mp4']

threads = []
files = range(len(list))
#创建线程
for i in files:
  t = threading.Thread(target=player,args=(list[i],))
  threads.append(t)
if __name__ == '__main__':
  #启动线程
  for i in files:
    threads[i].start()
  for i in files:
    threads[i].join()

  #主线程
print 'end:%s' %ctime()

 

 

 

 

 有趣的是我们又创建了一个player()函数,这个函数用于判断播放文件的类型。如果是mp3 格式的,我们将调用music()函数,如果是mp4 格式的我们调用move()函数。哪果两种格式都不是那么只能告诉用户你所提供有文件我播放不了。
然后,我们创建了一个list 的文件列表,注意为文件加上后缀名。然后我们用len(list) 来计算list列表有多少个文件,这是为了帮助我们确定循环次数。
接着我们通过一个for 循环,把list 中的文件添加到线程中数组threads[]中。接着启动threads[]线程组,最后打印结束时间。

 

 

通过上面的程序,我们发现player()用于判断文件扩展名,然后调用music()和move() ,其实,music()和move()完整工作是相同的,我们为什么不做一台超级播放器呢,不管什么文件都可以播放。再次经过改造,我们的超级播放器诞生了。

 

from time import sleep, ctime
import threading
#创建超级播放器
def super_player(file,time):
  for i in range(2):
    print 'Start playing: %s! %s' %(file,ctime())
    sleep(time)
#播放的文件与播放时长
list = {'爱情买卖.mp3':3,'阿凡达.mp4':5,'我和你.mp3':4}
threads = []
files = range(len(list))
#创建线程
for file,time in list.items():
  t = threading.Thread(target=super_player,args=(file,time))
  threads.append(t)
if __name__ == '__main__':
  #启动线程
  for i in files:
  threads[i].start()
  for i in files:
  threads[i].join()
  #主线程
print 'end:%s' %ctime()

 

 除了使用Python 所提供的线程类外,我们也可以根据需求来创建自己的线程类。

import threading
from time import sleep, ctime
#创建线程类
class MyThread(threading.Thread):
    def __init__(self,func,args,name=''):
    threading.Thread.__init__(self)
    self.name=name
    self.func=func
    self.args=args

def run(self):
    apply(self.func,self.args)

def super_play(file,time):
    for i in range(2):
        print 'Start playing: %s! %s' %(file,ctime())
        sleep(time) 

list = {'爱情买卖.mp3':3,'阿凡达.mp4':5}
#创建线程
threads = []
files = range(len(list))
for file,time in list.items():
  t = MyThread(super_play,(file,time),super_play.__name__)
  threads.append(t)

if __name__ == '__main__':
  #启动线程
  for i in files:
    threads[i].start()
  for i in files:
    threads[i].join()
#主线程
print 'end:%s' %ctime()

 

 

 MyThread(threading.Thread)
创建MyThread 类,用于继承threading.Thread 类。
__init__() 类的初始化方法对func、args、name 等参数进行初始化。apply() 当函数参数已经存在于一个元组或字典中时,间接地调用函数。args 是一个包含将要提供给函数的按位置传递的参数的元组。如果省略了args,任何参数都不会被传递,kwargs 是一个包含关键字参数的字典。
由于MyThread 类继承threading.Thread 类,所以,我们可以使用MyThread 类来创建线程。

posted @ 2017-10-11 11:04  botoo  阅读(511)  评论(0编辑  收藏  举报