#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. Volley 源码解析 StringRequest解析

    Android Vollety是一个很有用的框架,所以想借鉴前人思想,分析这个源代码. 参考: http://blog.csdn.net/crazy__chen/article/details/464 ...

  2. Codeforces D. Iahub and Xors

    题目大意:给定一个N*N的区间,1:对(x0,y0,x1,y1)每个直 都xor v: 2: 求(x0,y0,x1,y1)区间的 sum xor: http://codeforces.com/blog ...

  3. 洛谷P1865 A % B Problem

    1.洛谷P1865 A % B Problem 题目背景 题目名称是吸引你点进来的 实际上该题还是很水的 题目描述 区间质数个数 输入输出格式 输入格式: 一行两个整数 询问次数n,范围m 接下来n行 ...

  4. 洛谷 P4318 完全平方数

    题目描述 小 X 自幼就很喜欢数.但奇怪的是,他十分讨厌完全平方数.他觉得这些数看起来很令人难受.由此,他也讨厌所有是完全平方数的正整数倍的数.然而这丝毫不影响他对其他数的热爱. 这天是小X的生日,小 ...

  5. [转] java中volatile关键字的含义

    在java线程并发处理中,有一个关键字volatile的使用目前存在很大的混淆,以为使用这个关键字,在进行多线程并发处理的时候就可以万事大吉. Java语言是支持多线程的,为了解决线程并发的问题,在语 ...

  6. MAPZONE GIS SDK接入Openlayers3之五——图形编辑工具

    图形编辑工具提供对要素图形进行增.删.改的功能,具体包括以下几种工具类型: 浏览工具 选择工具 创建要素工具 删除命令 分割工具 合并命令 节点编辑工具 修边工具 撤销命令 重做命令 工具的实现基本上 ...

  7. RNN与LSTM

    Recurrent Neural Networks Recurrent neural networks are networks with loops in them, allowing inform ...

  8. HDU 4902 Nice boat 线段树+离线

    据说暴力也过了.还傻逼地写了这么长. . . #include <stdio.h> #include <string.h> #include <math.h> #i ...

  9. weexapp 开发流程(一)开发环境配置

    1.创建项目 weexpack create weexapp 2.安装必要插件 npm i jwt-simple vue-resource vue-router vuex vuex-router-sy ...

  10. free命令具体解释——Linux性能分析

    一.使用格式 语法格式:free [-b | -k | -m] [-o] [-s delay ] [-t] [-l] [-V] [-b | -k | -m] :选择数据的单位-b字节.-k千字节.-m ...