多线程-threading模块2
#coding:utf-8
import threading
from time import sleep,ctime #音乐播放器
def music(func):
for i in range(2):
print('Start playing: %s! %s' %(func,ctime()))
sleep(2) #视频播放器
def move(func):
for i in range(2):
print('Start playing: %s! %s' % (func, ctime()))
sleep(5) #判断文件类型,并交给相应的函数执行
def player(name):
r = name.split('.')[1]
if r == 'mp3':
music(name)
elif r == 'mp4':
move(name)
else:
print('error: The format is not recognized!') list = ['爱情买卖.mp3','阿凡达.mp4'] #创建线程数组
threads = []
files = range(len(list)) for i in files:
t = threading.Thread(target=player,args=(list[i],))
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())
运行结果:
Start playing: 爱情买卖.mp3! Thu Oct 18 09:53:33 2018
Start playing: 阿凡达.mp4! Thu Oct 18 09:53:33 2018
Start playing: 爱情买卖.mp3! Thu Oct 18 09:53:35 2018
Start playing: 阿凡达.mp4! Thu Oct 18 09:53:38 2018
all end: Thu Oct 18 09:53:43 2018
然后,我们创建了一个 list 的文件列表,注意为文件加上后缀名。然后我们用 len(list) 来计算 list 列表有多少个文件,这是为了帮助我们确定循环次数。
接着我们通过一个 for 循环,把 list 中的文件添加到线程中数组 threads[]中。接着启动 threads[] 线程组,最后打印结束时间。
多线程-threading模块2的更多相关文章
- 再看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模块3
超级播放器 #coding:utf-8 import threading from time import sleep,ctime #超级播放器 def super_player(file,time) ...
- Python:使用threading模块实现多线程编程
转:http://blog.csdn.net/bravezhe/article/details/8585437 Python:使用threading模块实现多线程编程一[综述] Python这门解释性 ...
随机推荐
- NOIPSB评测机+SB题DAY2
忍者钩爪 题目描述 小 Q 是一名酷爱钩爪的忍者, 最喜欢飞檐走壁的感觉, 有一天小 Q 发现一个练习使用钩 爪的好地方,决定在这里大显身手. 场景的天花板可以被描述为一个无穷长的数轴, 初始小 Q ...
- Hadoop安装和基本单机部署
下载安装 # 下载 $ cd /usr/local $ wget http://mirrors.hust.edu.cn/apache/hadoop/common/hadoop-2.9.2/hadoo ...
- mysqldump 把数据库备份到异地的服务器
原文:http://www.open-open.com/code/view/1420121471484 这个方法可以把通过mysqldump 把本地数据库备份到远端主机, 中间数据的传输通过 ssh ...
- 应用程序中的server错误,没有名称为“ServiceBehavior”的服务行为
应用程序中的server错误,没有名称为"ServiceBehavior"的服务行为 今天在阅读"创建和使用Web服务"的相关内容,在浏览器中查 ...
- 【转载】容器技术 & Docker & 与虚拟化的比较
看到10月份天天写博客,只有一天没写,非常棒! 11月份也基本每天都写,现在看到有三天没加新博客,应该是之前挖的坑太多了,需要填坑,呵呵. 那这篇文章是不是为了占坑呢?哈哈.我不说话. 容器技术,这篇 ...
- [Tools] Region commands to collapse the code by group
For a file which contians lots of lines of code, we can use 'comments region' to collapse the code. ...
- [React] Use React.memo with a Function Component to get PureComponent Behavior
A new Higher Order Component (HOC) was recently released in React v16.6.0 called React.memo. This be ...
- eclipse 配置执行hadoop 2.7 程序样例參考步骤
前提:你搭建好了hadoop 2.x的linux环境,并可以成功执行.还有就是window可以訪问到集群.over 1. hfds-site.xml 添加属性:关闭集群的权限校验.windows的用户 ...
- MySQL和MongoDB的性能测试
软硬件环境 MySQL版本:5.1.50,驱动版本:5.1.6(最新的5.1.13有很多杂七杂八的问题) MongoDB版本:1.6.2,驱动版本:2.1 操作系统:Windows XP SP3(这个 ...
- codeforces#FF(div2) D DZY Loves Modification
首先要知道选择行列操作时顺序是无关的 用两个数组row[i],col[j]分别表示仅选择i行能得到的最大值和仅选择j列能得到的最大值 这个用优先队列维护,没选择一行(列)后将这行(列)的和减去对应的n ...