多线程-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())
和 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
多线程-threading模块3的更多相关文章
- 再看python多线程------threading模块
现在把关于多线程的能想到的需要注意的点记录一下: 关于threading模块: 1.关于 传参问题 如果调用的子线程函数需要传参,要在参数后面加一个“,”否则会抛参数异常的错误. 如下: for i ...
- 多线程threading模块
python的多线程编程 简介 多线程编程技术可以实现代码并行性,优化处理能力,同时功能的更小划分可以使代码的可重用性更好.Python中threading和Queue模块可以用来实现多线程编程. 详 ...
- Python:多线程threading模块
目录 Thread对象 Lock对象 local对象 Thread对象: 多任务可以由多进程完成,也可以由一个进程内的多线程完成.进程是由至少1个线程组成的. threading模块在较低级的模块 _ ...
- python编程中的并发------多线程threading模块
任务例子:喝水.吃饭动作需要耗时1S 单任务:(耗时20s) for i in range(10): print('a正在喝水') time.sleep(1) print('a正在吃饭') time. ...
- 多线程-threading模块
#coding:utf-8 import threading from time import sleep,ctime #音乐播放器 def music(func): for i in range(2 ...
- Python_多线程threading模块
python 在执行的时候会淡定的在CPU上只允许一个线程运行,故Python在多核CPU的情况下也只能发挥出单核的功能,其中的原因:gil锁 gil 锁 (全局解释器锁):每个线程在执行时都需要先获 ...
- Python(多线程threading模块)
day27 参考:http://www.cnblogs.com/yuanchenqi/articles/5733873.html CPU像一本书,你不阅读的时候,你室友马上阅读,你准备阅读的时候,你室 ...
- 多线程-threading模块2
从上面例子中发现线程的创建是颇为麻烦的,每创建一个线程都需要创建一个 t(t1.t2....),如果创建的线程较多时这样极其不方便.下面对通过例子进行改进: #coding:utf-8 impor ...
- Python:使用threading模块实现多线程编程
转:http://blog.csdn.net/bravezhe/article/details/8585437 Python:使用threading模块实现多线程编程一[综述] Python这门解释性 ...
随机推荐
- raspberry pi系统安装
1.格式化SD卡,用SDFormatter 2.解压下载的操作系统 3.复制操作系统到SD卡(要放在根目录,把最外面的文件夹路径去掉) 4.把SD卡插入raspberry pi,接上电源 5.在启动界 ...
- NOIP临考经验(转)
[COGS]NOIP临考经验 1. 提前15分钟入场,此时静坐调整心态,适当的深呼吸 2. 打开编辑器并调整为自己喜欢的界面 3. 熟悉文件目录,写好准确无误的代码模板 4. 压缩包或许还不能 ...
- python的websocket实现Tornado
1.使用flask的扩展: pip install flask-socketio 2.Tornado提供较好的ws(websocket)支持 参考:1.http://www.jianshu.com/p ...
- nuxt.js 加百度统计
Mark一下: 在 Nuxt.js应用中使用Google统计分析服务,或者百度统计分析服务,推荐在 plugins 目录下创建 plugins/ga.js 文件.统计统计分析我们可以获取网站pv,uv ...
- Git checkout on a remote branch does not work
I believe this occurs when you are trying to checkout a remote branch that your local git repo is no ...
- 安卓开发懒鬼最爱之ButterKnife,依赖注入第三方是库,进一步加速开发速度
转载请注明出处:王亟亟的大牛之路 还在烦躁一大堆findById的控件操作而烦恼么? 平时,我们的那一系列findById是一个"浩大的project"样比例如以下 这是以前一个项 ...
- SQL server 子查询 链接查询
数据库 if while else 的使用 数据库运算符优先级
- 查看windows下port占用
有时假tomcat启动时提示port被占用.能够用netstat -aon|findstr "8080" 命令查找到,然后 用 taskkill /f /pid pid号 强制ki ...
- Linux MySQL主从复制(Replication)配置
MySQL是开源的关系型数据库系统.复制(Replication)是从一台MySQL数据库服务器(主服务器master)复制数据到另一个服务器(从服务器slave)的一个进程. 配置主服务器(mast ...
- mysql语句:SET NAMES UTF8
一直以来只知道mysql_query("SET NAMES UTF8");是设定数据库编码的,但是一直不清楚“SET NAMES UTF8”是什么. 直到今天才知道 SET NAM ...