多线程-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这门解释性 ...
随机推荐
- 使用git 高效多人合作
复习一下... 附加学习链接: http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/) ...
- 如何在ASP.NET Core自定义中间件中读取Request.Body和Response.Body的内容?
原文:如何在ASP.NET Core自定义中间件中读取Request.Body和Response.Body的内容? 文章名称: 如何在ASP.NET Core自定义中间件读取Request.Body和 ...
- 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 ...
- chrome 的onbeforeunload事件没有触发
onbeforeunload event is not working when user not click inside the body of page 0down votefavorite ...
- ubutu强制结束进程 kill -9 ProcessID
强制终止进程 kill -9 2128 表示强制结束进程号 2128 对应的进程.
- Android实现SQLite数据库的增、删、改、查的操作
核心代码DAO类 package com.examp.use_SQLite.dao; import java.util.ArrayList; import java.util.List; import ...
- react 创建组件 (四)Stateless Functional Component
上面我们提到的创建组件的方式,都是用来创建包含状态和用户交互的复杂组件,当组件本身只是用来展示,所有数据都是通过props传入的时候,我们便可以使用Stateless Functional Compo ...
- 【转载】关于Hash
这个HASH算法不是大学里数据结构课里那个HASH表的算法.这里的HASH算法是密码学的基础,比较常用的有MD5和SHA,最重要的两条性质,就是不可逆和无冲突.所谓不可逆,就是当你知道x的HASH值, ...
- ZOJ 3230 Solving the Problems(数学 优先队列啊)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3230 Programming is fun, Aaron is ...
- SGU 194 Reactor Cooling 无源汇带上下界可行流
Reactor Cooling time limit per test: 0.5 sec. memory limit per test: 65536 KB input: standard output ...