多线程02

# -*- coding: utf-8 -*-
#python 27
#xiaodeng
#线程和进程
#http://www.cnblogs.com/fnng/p/3691053.html



#多线程:
from time import sleep,ctime
import threading

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

def music(func):
    for i in range(2):
        print 'Start playing:%s.%s'%(func,ctime())
        sleep(2)

def player(name): 
    r=name.split('.')[1]

    if r=='mp3':
        music(name)
    else:
        if r=='mp4':
            move(name)
        else:
            print 'error:the format is not recognized!'


list=[u'爱情买卖.mp3',u'大秦帝国.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())

 

posted @ 2015-11-05 20:01  Xiao|Deng  阅读(135)  评论(0编辑  收藏  举报