多线程-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这门解释性 ...
随机推荐
- ubuntu安装软件或upgrade出现 You might want to run 'apt-get -f install' to correct these
今天在ubuntu下安装任何软件都提示以下错误: You might want to run 'apt-get -f install' to correct these:The following p ...
- 纯Java实现定时任务(转)
第一种: import java.util.Timer; import java.util.TimerTask; /** * * 于第一种方式相比,优势 1>当启动和去取消任务时可以控制 2&g ...
- Adobe Flash builder破解方法
Flash Builder 4 有许多新的特性,可以结合新的功能使用新的Flex 4框架创建出更炫的应用.基于用户的反馈,对以数据中心的开发也进行了优化:对类如配置从服务器返回的数据类型这样的任务,也 ...
- 6.非关系型数据库(Nosql)之mongodb:集群(主从复制)
1.主从复制是MongoDB最经常使用的复制方式.这样的方式很灵活,可用于备份.故障恢复.读扩展等 2最主要的设置方式就是建立一个主节点和一个或多个从节点,每一个从节点要知道主节点的地址. 执行 ...
- saltstack安装配置(master&minion)
操作系统centos6.3,centos6.4,windows server2008R2,windows7. 文中的下载链接可能会随着saltstack官网上版本的更新,而出现变动或错误,请以你需要安 ...
- 浅谈JavaScript的Canvas(绘制图形)
HTML5中新增加的一个元素canvas,要使用canvas元素,浏览器必须支持html5.通过canvas标签来创建元素,并需要为canvas指定宽度和高度,也就是绘图区域的大小. <canv ...
- org.hibernate.id.IdentifierGenerationException错误解决方法
org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before ...
- 细谈SetButtonInfo函数及其用途
SetButtonInfo用于设置某个按钮,它的接口定义如下: 下面是它的几个接口函数说明: void CToolBar::SetButtonInfo(int nIndex, UINT nID, UI ...
- Spring在3.1版本后的bean获取方法的改变
xml配置不变,如下 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="ht ...
- XML-RPC JSON-RPC RPC是实现思路
XML-RPC - Wikipedia https://en.wikipedia.org/wiki/XML-RPC JSON-RPC - Wikipedia https://en.wikipedia. ...