threading 常见用法:

(1) 在 thread 中,我们是通过 thread.start_new_thread(function, args) 来开启一个线程,接收一个函数和该函数的参数,函数的参数必须是元组的形式
(2) 在 threading 中,我们是通过 threading.Thread(target=function, args=(....)) 来创建一个对象,然后再通过对象的 start() 方法来开启一个线程,多个线程可以并发执行

#!/usr/bin/env python
#-*- coding:utf-8 -*- import time
import threading def fun():
print time.ctime()
time.sleep(1) for i in range(3):
t = threading.Thread(target=fun, args=()) # 创建一个 Thread 对象,接收一个函数和该函数的参数
t.start() # 通过对象来开启一个新的线程,通过线程来执行函数
[root@localhost ~]$ python 1.py    # 多个线程并发执行,只需要执行一秒
Mon Jan 28 21:15:35 2019
Mon Jan 28 21:15:35 2019
Mon Jan 28 21:15:35 2019

threading 有两种方法可以开启一个新的线程:

(1) 如上面的例子,通过创建 Thread 对象,然后通过对象的 start() 方法来运行一个新的线程
(2) 第二种方法是继承 threading.Thread 这个类, 然后重写 run() 方法来运行一个新的线程

#!/usr/bin/env python
#-*- coding:utf-8 -*- import time
import threading class MyThread(threading.Thread): def __init__(self):
threading.Thread.__init__(self) def run(self):
print time.ctime()
time.sleep(1) for i in range(3):
t = MyThread()
t.start()
[root@localhost ~]$ python 1.py
Mon Jan 28 23:53:02 2019
Mon Jan 28 23:53:02 2019
Mon Jan 28 23:53:02 2019

threading.Lock():用于创建一个锁对象,我们可以同时开启多个线程,但是在任意时刻只能有一个线程能在解释器运行,因此需要由全局解锁器(GIL)控制运行哪个线程

锁对象的常用方法如下:

lock = threading.Lock() 创建一个锁对象
    lock.acquire() 锁上锁,当某个线程被锁上之后,会优先执行这个锁的内容,直到锁释放才会执行其他线程
    lock.locked() 查看锁的状态,如果已经被锁上了则返回True,否则返回False
    lock.release() 释放锁,必须先获得锁才能释放锁

#!/usr/bin/env python
#-*- coding:utf-8 -*- import time
import threading def hello():
for i in range(3):
hello_lock.acquire()
print('hello'),
time.sleep(1)
world_lock.release() def world():
for i in range(3):
world_lock.acquire()
print('world')
time.sleep(1)
hello_lock.release() hello_lock = threading.Lock()
world_lock = threading.Lock() world_lock.acquire()
t1 = threading.Thread(target=hello, args=())
t2 = threading.Thread(target=world, args=()) t1.start()
t2.start()
[root@localhost ~]$ python 1.py
hello world
hello world
hello world

其他常用方法:

t.join()方法用于阻塞下一个线程的运行,等待当前线程执行完再执行下一个线程,例子如下:

#!/usr/bin/env python
#-*- coding:utf-8 -*- import time
import threading def fun():
print time.ctime()
time.sleep(1) for i in range(3):
t = threading.Thread(target=fun, args=())
t.start()
t.join()
[root@localhost ~]$ python 1.py     # 由于线程被阻塞,需要执行3秒
Mon Jan 28 21:33:35 2019
Mon Jan 28 21:33:36 2019
Mon Jan 28 21:33:37 2019

t.setDaemon()方法用于设置当前线程随着主线程的退出而退出,例子如下:

#!/usr/bin/env python
#-*- coding:utf-8 -*- import time
import threading def fun(): # 函数的功能是打印三次当前时间
for i in range(3):
print time.ctime()
time.sleep(1) t = threading.Thread(target=fun, args=())
t.setDaemon(True)
t.start() print 'End...' # 执行 print 之后,主线程就退出了,最终结果只会打印一次
[root@localhost ~]$ python 1.py
Mon Jan 28 21:54:48 2019
End...

t.getName() 用于获取线程的名字,t.setName() 用于重新设置线程的名字,默认的线程名是 Thread-1,Thread-2,Thread-3,......

#!/usr/bin/env python
#-*- coding:utf-8 -*- import time
import threading def fun():
newName = 'new-' + t.getName()
print newName
time.sleep(1) for i in range(3):
t = threading.Thread(target=fun, args=())
t.start()
[root@localhost ~]$ python 1.py
new-Thread-1
new-Thread-2
new-Thread-3

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

  1. 基于Python的多线程模块Threading小结

    步入正题前,先准备下基本知识,线程与进程的概念. 相信作为一个测试人员,如果从理论概念上来说其两者的概念或者区别,估计只会一脸蒙蔽,这里就举个例子来说明下其中的相关概念. 平安夜刚过,你是吃到了苹果还 ...

  2. Python并发复习2 - 多线程模块threading

    一.多线程的调用 threading 模块建立在thread 模块之上.thread模块以低级.原始的方式来处理和控制线程,而threading 模块通过对thread进行二次封装, 提供了更方便的a ...

  3. 用生动的案例一步步带你学会python多线程模块

    鱼和熊掌不可兼得 鱼,我所欲也,熊掌,亦我所欲也,二者不可得兼,舍鱼而取熊掌者也. 从6月开始写公众号,连着四个月一直尽量保证一周五更,结果整天熬夜搞的身体素质骤降.十一休假决定暂时将公众号放放,好好 ...

  4. python笔记9 线程进程 threading多线程模块 GIL锁 multiprocessing多进程模块 同步锁Lock 队列queue IO模型

    线程与进程 进程 进程就是一个程序在一个数据集上的一次动态执行过程.进程一般由程序.数据集.进程控制块三部分组成.我们编写的程序用来描述进程要完成哪些功能以及如何完成:数据集则是程序在执行过程中所需要 ...

  5. python-网络安全编程第六天(threading多线程模块&Queue模块&subprocess模块)

    前言 昨天晚上9点多就睡了 2点起来没睡意... 那就学习吧emmmm ,拿起闲置几天的python课程学习.学习到现在5.58了 总结下 继续开始学习新的内容 多多线程? 线程(英语:thread) ...

  6. Python第十五天 datetime模块 time模块 thread模块 threading模块 Queue队列模块 multiprocessing模块 paramiko模块 fabric模块

    Python第十五天  datetime模块 time模块   thread模块  threading模块  Queue队列模块  multiprocessing模块  paramiko模块  fab ...

  7. Python线程模块threading

    线程,程序执行的最小单元,单线程处理多个任务只能一个处理完后继续处理下一个直到全部处理完,多线程处理任务会比单线程处理起来快吗?在python程序里得看情况,首先有GIL锁的存在导致同一时刻只能有一个 ...

  8. Python多线程模块

    引言 thread threading 1 Thread 11 下面使用threading模块实现与上面相同的功能 12 在创建新线程时还可以给Thread传递可调用类的对象这样使用类本身来保存信息 ...

  9. 多线程模块的同步机制event对象

    多线程模块的同步机制event对象 线程的核心特征就是他们能够以非确定的方式(即何时开始执行,何时被打断,何时恢复完全由操作系统来调度管理,这是用户和程序员无法确定的)独立执行的,如果程序中有其他线程 ...

  10. Python的多线程(threading)与多进程(multiprocessing )

    进程:程序的一次执行(程序载入内存,系统分配资源运行).每个进程有自己的内存空间,数据栈等,进程之间可以进行通讯,但是不能共享信息. 线程:所有的线程运行在同一个进程中,共享相同的运行环境.每个独立的 ...

随机推荐

  1. 内存单元,CPU对存储器的读写,三大总线

    .存储单元 存储器被分成若干个存储单元,每个存储单元从0开始顺序编号.电子计算机的最小信息单位是bit.8个bit组成一个Byte(虽然现在一个字节可能不是8位,有宽字符出现了,但是一般我们的机器都是 ...

  2. [转]SQL Server 2012 的 T-SQL 新功能 – 新的数据分析函数(LEAD、LAG)

    当您需要在 SQL Server 中利用 T-SQL 比较结果集的每一列跟前一列或后一列的差异时,在过去可能需要利用 CURSOR 搭配临时表变量,或是透过递归 CTE 来达到这个效果,如今 SQL ...

  3. Java项目中如何扩展第三方jar包中的类?

    有些时候你对第三方得到jar包中的类并不是很满意,想根据实际情况做一些扩展.如果说第三方的jar包已经提供了一些可扩展的类,比如提供了Interceptor,Filter或者其他的类,那么使用原生的比 ...

  4. Linux 网络子系统之网络协议接口层(一)

    Linux 网络设备驱动之网络协议接口层介绍. 网络协议接口层最主要的功能是给上层协议提供透明的数据包发送和接收接口. 当上层ARP或IP需要发送数据包时,它将调用网络协议接口层的dev_queue_ ...

  5. hive & hive beeline常用参数

    Hive 1参数如下: usage: hive -d,--define <key=value> Variable substitution to apply to Hive command ...

  6. [转]eclipse导入V7包出现错误解决办法

    android下v4    v7   v21等包是android系统的扩展支持包,就想windows的系统补丁一个道理. android的扩展包主要是用来兼容低版本的,比如android3.0以后出现 ...

  7. style="display:none"隐藏html的标签

    隐藏html的标签 <div class="span11 alignment"> <h1>我的虚拟网络</h1> </div> &l ...

  8. libmysqlclient16 libmysqlclient-dev

    如果提示安装 libmysqlclient16 则用 libmysqlclient-dev 代替之

  9. js对数字的校验

    //-----------------------------------------------函数(1):允许输入正数和负数的表达式-------------------------------- ...

  10. 利用层的table-row、table-cell属性进行页面布局

    利用层的table-row.table-cell属性可以进行等高.宽度自适应页面布局,这是参看了<我所知道的几种display:table-cell的应用>及<基于display:t ...