zoukankan      html  css  js  c++  java
  • 多线程-threading模块3

    超级播放器

    #coding:utf-8
    import threading
    from time import sleep,ctime
    
    #超级播放器
    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':2}
    
    
    
    #创建线程数组
    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('all end: %s' %ctime())
    首先创建字典 list ,用于定义要播放的文件及时长(秒),通过字典的 items()方法来循环的取 file

    和 time,取到的这两个值用于创建线程。


    接着创建 super_player()函数,用于接收 file 和 time,用于确定要播放的文件及时长。 最后是线程启动运行。运行结果:

    Start playing: 爱情买卖.mp3! Thu Oct 18 10:06:57 2018
    Start playing: 阿凡达.mp4! Thu Oct 18 10:06:57 2018
    Start playing: 我和你.mp3! Thu Oct 18 10:06:57 2018
    Start playing: 我和你.mp3! Thu Oct 18 10:06:59 2018
    Start playing: 爱情买卖.mp3! Thu Oct 18 10:07:00 2018
    Start playing: 阿凡达.mp4! Thu Oct 18 10:07:02 2018
    all end: Thu Oct 18 10:07:07 2018
  • 相关阅读:
    浅拷贝与深拷贝的实现方式、区别;deepcopy如果你来设计,如何实现(一)
    可变与不可变类型(一)
    查看虚拟机版本
    无法应用原保存的显示器配置
    uname -a输出内容分析
    Python 模块
    Python 函数(三)
    Python 函数(二)
    Python 函数(一)
    5-26 单词长度
  • 原文地址:https://www.cnblogs.com/yrxns/p/9808927.html
Copyright © 2011-2022 走看看