多线程-threading模块3
2024-09-30 01:19:51
超级播放器
#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
最新文章
- DDD设计中的Unitwork与DomainEvent如何相容?
- javascript “||”、“&;&;”的灵活运用
- PHP面向对象中常用的关键字和魔术方法
- mysql_fetch_row()与mysql_fetch_array()的使用介绍
- find 日常使用
- hibernate_validator_01
- FileAccess枚举
- HTML8表单验证
- C#使用Xamarin开发可移植移动应用(5.进阶篇显示弹出窗口与通讯中心)附源码
- kappa系数在评测中的应用
- Windows Cluster 添加新节点--验证报错
- 阿里云HBase携X-Pack再进化,重新赋能轻量级大数据平台
- KMP 求最小循环节
- eclipse maven引入第三方jar包后如何下载源代码(sources)
- spring boot 邮件发送(带附件)
- php--------对象(object) 与 数组(array) 的转换
- Ceph块存储介绍
- Unity鼠标点击Collider
- 3dContactPointAnnotationTool开发日志(二三)
- java创建web服务
热门文章
- #ifdef #endif #if #endif
- 第1章 SpringBoot 简介
- File类 文件过滤器
- hdu5289(2015多校1)--Assignment(单调队列)
- SPOJ VLATTICE Visible Lattice Points (莫比乌斯反演基础题)
- 【健康生活】Google、百度之间的选择
- Spark 学习笔记:(四)MLlib基础
- Swift开发教程--怎样播放图片动画
- liberOJ #6173. Samjia 和矩阵 hash+后缀数组
- Space for commit to queue couldn't be acquired