• 多线程-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
  • 相关阅读:
    软件测试课堂练习
    JSP第一次作业
    安卓第六次作业
    安卓第五次作业
    第四次安卓作业
    JSP第四周
    软件测试课堂练习3.4
    Android数据库表
    Android购物菜单
    Android增删改查
  • 原文地址:https://www.cnblogs.com/yrxns/p/9808927.html
Copyright © 2020-2023  润新知