#coding:utf-8
import threading
from time import sleep,ctime #音乐播放器
def music(func):
for i in range(2):
print('I was listening to %s! %s' %(func,ctime()))
sleep(2) #视频播放器
def move(func):
for i in range(2):
print('I was at the %s! %s' %(func,ctime()))
sleep(5) #创建线程数组
threads = [] #创建线程t1,并添加到线程数组
t1 = threading.Thread(target=music,args=(u'爱情买卖',))
threads.append(t1) #创建线程t2,并添加到线程数组
t2 = threading.Thread(target=move,args=(u'阿凡达',))
threads.append(t2) if __name__ == "__main__":
#启动线程
for i in threads:
i.start() #守护线程
for i in threads:
i.join() print('all end: %s' %ctime())

运行结果:

I was listening to 爱情买卖! Thu Oct 18 09:30:37 2018
I was at the 阿凡达! Thu Oct 18 09:30:37 2018
I was listening to 爱情买卖! Thu Oct 18 09:30:39 2018
I was at the 阿凡达! Thu Oct 18 09:30:42 2018
all end: Thu Oct 18 09:30:47 2018

说明:

import threading 引入线程模块。
threads = [] 创建线程数组,用于装载线程。
 
 
threading.Thread()通过调用 threading 模块的 Thread()方法来创建线程。
 
 
start() 开始线程活动。

join() 等待线程终止。

通过 for 循环遍历 thread 数组中所装载的线程;然后通过 start()函数启动每一个线程。

join()会等到线程结束,或者在给了 timeout 参数的时候,等到超时为止。join()的另一个比较重 要的方面是它可以完全不用调用。一旦线程启动后,就会一直运行,直到线程的函数结束,退出为止。

多线程-threading模块的更多相关文章

  1. 再看python多线程------threading模块

    现在把关于多线程的能想到的需要注意的点记录一下: 关于threading模块: 1.关于 传参问题 如果调用的子线程函数需要传参,要在参数后面加一个“,”否则会抛参数异常的错误. 如下: for i ...

  2. 多线程threading模块

    python的多线程编程 简介 多线程编程技术可以实现代码并行性,优化处理能力,同时功能的更小划分可以使代码的可重用性更好.Python中threading和Queue模块可以用来实现多线程编程. 详 ...

  3. Python:多线程threading模块

    目录 Thread对象 Lock对象 local对象 Thread对象: 多任务可以由多进程完成,也可以由一个进程内的多线程完成.进程是由至少1个线程组成的. threading模块在较低级的模块 _ ...

  4. python编程中的并发------多线程threading模块

    任务例子:喝水.吃饭动作需要耗时1S 单任务:(耗时20s) for i in range(10): print('a正在喝水') time.sleep(1) print('a正在吃饭') time. ...

  5. Python_多线程threading模块

    python 在执行的时候会淡定的在CPU上只允许一个线程运行,故Python在多核CPU的情况下也只能发挥出单核的功能,其中的原因:gil锁 gil 锁 (全局解释器锁):每个线程在执行时都需要先获 ...

  6. Python(多线程threading模块)

    day27 参考:http://www.cnblogs.com/yuanchenqi/articles/5733873.html CPU像一本书,你不阅读的时候,你室友马上阅读,你准备阅读的时候,你室 ...

  7. 多线程-threading模块3

    超级播放器 #coding:utf-8 import threading from time import sleep,ctime #超级播放器 def super_player(file,time) ...

  8. 多线程-threading模块2

    从上面例子中发现线程的创建是颇为麻烦的,每创建一个线程都需要创建一个 t(t1.t2....),如果创建的线程较多时这样极其不方便.下面对通过例子进行改进:   #coding:utf-8 impor ...

  9. Python:使用threading模块实现多线程编程

    转:http://blog.csdn.net/bravezhe/article/details/8585437 Python:使用threading模块实现多线程编程一[综述] Python这门解释性 ...

随机推荐

  1. ssh的安装和使用

    1.ssh的安装 服务器端:sudo apt-get install openssh-serve 客户端:sudo apt-get install openssh-client 2.ssh的操作 查看 ...

  2. Codeforces 12 D Ball

    Discription N ladies attend the ball in the King's palace. Every lady can be described with three va ...

  3. Java下接口interface前面要不要加I

    说明:加I和不加I都可以,看需要,没有强制要求. 在Java中更多是提倡不加I的,可以看下JDK的源码,都是不加I的. 微软C#是规定要加I,这也是影响从而导致有这个话题的原因. Java中特定不直接 ...

  4. UICollectionView 使用 介绍

    1.1. Collection View 全家福: UICollectionView, UITableView, NSCollectionView n   不直接等效于NSCollectionView ...

  5. Mysql数据库中CURRENT_TIMESTAMP和ON UPDATE CURRENT_TIMESTAMP区别

    如图所示,mysql数据库中,当字段类型为timestamp时,如果默认值取CURRENT_TIMESTAMP,则在insert一条记录时,end_time的值自动设置为系统当前时间,如果勾选了 ON ...

  6. DICOM医学图像显示算法改进与实现——LUT

    引言 随着Ul(超声成像).CT(计算机断层成像).MRI(核磁共振成像).CR(计算机X线成像).电子内窥镜.盯(正电子发射断层成像)和MI(分子影像)等医学影像设备不断涌现,利用计算机对医学影像设 ...

  7. [Adobe Analytics] Segments types

    There are three types of Segmentation Hit-based Visit-based Visitor-based There are four segment con ...

  8. [HTML5] Show Images of Differing Resolutions Depending on the Viewport Width with srcset

    For small viewports, we want to save bandwidth and we may be dealing with slow speeds; so it's very ...

  9. poj 1659 Frogs' Neighborhood 度序列可图化 贪心

    题意: 对一个无向图给出一个度序列,问他是否可简单图化. 分析: 依据Havel定理,直接贪心就可以. 代码: //poj 1659 //sep9 #include <iostream> ...

  10. 创建一个简单的 http 服务器

    创建一个简单的 http 服务器 直接在 目录下运行 当前的目录即可是root 目录 默认端口8000 应该可以加参数修改端口号 Python2 python -m SimpleHTTPServer ...