多线程-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这门解释性 ...
随机推荐
- 微软自带的异步Ajax请求
一.使用步骤 二.示例代码 using System; using System.Collections.Generic; using System.Linq; using System.Web; u ...
- dropwatch 网络协议栈丢包检查利器 与 火丁笔记
http://blog.yufeng.info/archives/2497 源码:http://git.fedorahosted.org/cgit/dropwatch.git http://blog. ...
- [vxlan] 二 什么是VXLAN
VXLAN是一种mac in UDP的技术.简单讲就是传统的二层帧被封装到了UDP的package中.通过UDP的IP网络发送到目的地然后再解封装. VXLAN 跟VLAN对比,最重要的一个概念就是V ...
- .net core mvc启动顺序以及主要部件1
原文:.net core mvc启动顺序以及主要部件1 首先我是新人一个写这些东西也是为了增加记忆,有不对的地方请多多指教. 说回正题,打开Program.cs文件,看到在有个CrateWebHost ...
- OSX: diskutil命令-转换成自由空间并再对其分区
声明:本文涉及的操作非常可能会破坏你的系统文件,造成数据丢失.请谨慎模仿,一切后果作者均不承担不论什么责任. 目的: 尽管说比較熟悉diskutil命令和它的GUI前端程序磁盘工具(Disk Util ...
- "听话"的品格的症状
反思了一下,也许是因为以前比较听话,听大人的话,听老师的话,听长辈的话.听电视剧的话..........哈哈 现在发现,世界是靠自己去认识,去体会的,别人的经验都只能作为参考,绝对不能不加思考的照搬硬 ...
- [ACM] ZOJ 3725 Painting Storages (DP计数+组合)
Painting Storages Time Limit: 2 Seconds Memory Limit: 65536 KB There is a straight highway with ...
- Cocoapods Undefined symbols for architecture armv7s\arm64
此类错误 "_OBJC_CLASS_$_AFURLSessionManager", referenced from: 解决的方法 在other linker flags里加入一行 ...
- ShopMall
https://github.com/KingsleyYau/ShopMall-Android https://github.com/KingsleyYau/ShopMall-iOS https:// ...
- lodar
一个文件使用另一个文件的东西的时候需要使用到 export{开放} --watch 自动检测 可以加载打包的后边 module.exports = { entry:{ //入口文件 home:&quo ...