python——threading模块
一、什么是线程
线程是操作系统能够进行运算调度的最小单位。进程被包含在进程中,是进程中实际处理单位。一条线程就是一堆指令集合。
一条线程是指进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务。
二、什么是进程
进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础。在早期面向进程设计的计算机结构中,进程是程序的基本执行实体;在当代面向线程设计的计算机结构中,进程是线程的容器。程序是指令、数据及其组织形式的描述,进程是程序的实体。
三、区别
进程(process)是一块包含了某些资源的内存区域。操作系统利用进程把它的工作划分为一些功能单元。
进程中所包含的一个或多个执行单元称为线程(thread)。进程还拥有一个私有的虚拟地址空间,该空间仅能被它所包含的线程访问。
线程只能归属于一个进程并且它只能访问该进程所拥有的资源。当操作系统创建一个进程后,该进程会自动申请一个名为主线程或首要线程的线程。
处理IO密集型任务或函数用线程;
处理计算密集型任务或函数用进程。
四、GIL
首先需要明确的一点是 GIL 并不是Python的特性,它是在实现Python解析器(CPython)时所引入的一个概念。就好比C++是一套语言(语法)标准,但是可以用不同的编译器来编译成可执行代码。有名的编译器例如GCC,INTEL C++,Visual C++等。Python也一样,同样一段代码可以通过CPython,PyPy,Psyco等不同的Python执行环境来执行。像其中的JPython就没有GIL。然而因为CPython是大部分环境下默认的Python执行环境。所以在很多人的概念里CPython就是Python,也就想当然的把 GIL 归结为Python语言的缺陷。所以这里要先明确一点:GIL并不是Python的特性,Python完全可以不依赖于GIL
那么CPython实现中的GIL又是什么呢?GIL全称 Global Interpreter Lock 为了避免误导,我们还是来看一下官方给出的解释:
In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple native threads from executing Python bytecodes at once. This lock is necessary mainly because CPython’s memory management is not thread-safe. (However, since the GIL exists, other features have grown to depend on the guarantees that it enforces.)
GIL限制:在同一时刻,只能有一个线程进入CPython解释器。
解决GIL限制的办法:
(1)Multiprocess
既然多线程不能同时进入Cpython解释器,那么,我们可以通过把多个线程放入不同进程中,让多进程进入Cpython解释器,分配给各个CUP,以利用多核实现并行。
(2)C
五、创建线程
导入模块threading,通过threading.Thread()创建线程。其中target接收的是要执行的函数名字,args接收传入函数的参数,以元组()的形式表示。
import threading def foo(n)
print("foo%s"%n)
t1 = threading.Thread(target=foo,args=(1,)) #创建线程对象
六、启动线程
通过线程对象t1.start()或t2.start()启动线程。
t1 = threading.Thread(target=sayhi, args=(1,)) # 生成一个线程实例
t2 = threading.Thread(target=sayhi, args=(2,)) # 生成另一个线程实例 t1.start() # 启动线程
t2.start() # 启动另一个线程
实例1:
import threading
import time def foo(n):
print("foo%s"%n)
time.sleep(1) def bar(n):
print("bar%s"%n)
time.sleep(2) t1 = threading.Thread(target=foo,args=(1,))
t2 = threading.Thread(target=bar,args=(2,)) t1.start()
t2.start() print("...in the main...")
结果:
程序启动后,主线程从上到下依次执行,t1、t2两个子线程启动后,与主线程并行,抢占CPU资源。因此,前三行的输出结果几乎同时打印,没有先后顺序,此时,需要等t1和t2都结束后程序才结束。故等待2s后,程序结束。程序总共花了2s。
foo1
bar2
...in the main... Process finished with exit code 0
实例2:
import threading
from time import ctime,sleep
import time def music(func):
for i in range(2):
print ("Begin listening to %s. %s" %(func,ctime()))
sleep(4)
print("end listening %s"%ctime()) def move(func):
for i in range(2):
print ("Begin watching at the %s! %s" %(func,ctime()))
sleep(5)
print('end watching %s'%ctime()) threads = []
t1 = threading.Thread(target=music,args=('七里香',))
threads.append(t1)
t2 = threading.Thread(target=move,args=('阿甘正传',))
threads.append(t2) if __name__ == '__main__': for t in threads:
t.start() print ("all over %s" %ctime())
结果:
Begin listening to 七里香. Thu Sep 29 14:21:55 2016
Begin watching at the 阿甘正传! Thu Sep 29 14:21:55 2016
all over Thu Sep 29 14:21:55 2016
end listening Thu Sep 29 14:21:59 2016
Begin listening to 七里香. Thu Sep 29 14:21:59 2016
end watching Thu Sep 29 14:22:00 2016
Begin watching at the 阿甘正传! Thu Sep 29 14:22:00 2016
end listening Thu Sep 29 14:22:03 2016
end watching Thu Sep 29 14:22:05 2016 Process finished with exit code 0
七、join()
在子线程执行完成之前,这个子线程的父线程将一直被阻塞。就是说,当调用join()的子进程没有结束之前,主进程不会往下执行。对其它子进程没有影响。
实例1:
import threading
from time import ctime,sleep
import time def music(func):
for i in range(2):
print ("Begin listening to %s. %s" %(func,ctime()))
sleep(4)
print("end listening %s"%ctime()) def move(func):
for i in range(2):
print ("Begin watching at the %s! %s" %(func,ctime()))
sleep(5)
print('end watching %s'%ctime()) threads = []
t1 = threading.Thread(target=music,args=('七里香',))
threads.append(t1)
t2 = threading.Thread(target=move,args=('阿甘正传',))
threads.append(t2) if __name__ == '__main__': for t in threads:
t.start()
t.join() print ("all over %s" %ctime())
结果解析:
t1线程启动→Begin listening→4s后end listening + Begin listening →4s后t2线程启动end listening t1结束 + Begin watching→5s后end listening + Begin watching→5s后end listening t2结束+ all over最后主进程结束。 就是酱紫,有点乱。。。
Begin listening to 七里香. Thu Sep 29 15:00:09 2016
end listening Thu Sep 29 15:00:13 2016
Begin listening to 七里香. Thu Sep 29 15:00:13 2016
end listening Thu Sep 29 15:00:17 2016
Begin watching at the 阿甘正传! Thu Sep 29 15:00:17 2016
end watching Thu Sep 29 15:00:22 2016
Begin watching at the 阿甘正传! Thu Sep 29 15:00:22 2016
end watching Thu Sep 29 15:00:27 2016
all over Thu Sep 29 15:00:27 2016
实例2:
import threading
from time import ctime,sleep
import time def music(func):
for i in range(2):
print ("Begin listening to %s. %s" %(func,ctime()))
sleep(4)
print("end listening %s"%ctime()) def move(func):
for i in range(2):
print ("Begin watching at the %s! %s" %(func,ctime()))
sleep(5)
print('end watching %s'%ctime()) threads = []
t1 = threading.Thread(target=music,args=('七里香',))
threads.append(t1)
t2 = threading.Thread(target=move,args=('阿甘正传',))
threads.append(t2) if __name__ == '__main__': for t in threads:
t.start()
t.join() #for循环的最后一次t的值,相当于t2 print ("all over %s" %ctime())
结果:
Begin listening to 七里香. Thu Sep 29 15:16:41 2016 #t1和t2线程启动
Begin watching at the 阿甘正传! Thu Sep 29 15:16:41 2016
end listening Thu Sep 29 15:16:45 2016
Begin listening to 七里香. Thu Sep 29 15:16:45 2016
end watching Thu Sep 29 15:16:46 2016
Begin watching at the 阿甘正传! Thu Sep 29 15:16:46 2016
end listening Thu Sep 29 15:16:49 2016 #t1结束
end watching Thu Sep 29 15:16:51 2016 #t2结束,t2结束之前,主线程一直被阻塞。t2结束主线程继续执行
all over Thu Sep 29 15:16:51 2016 #主线程结束
实例3:
import threading
from time import ctime,sleep
import time def music(func):
for i in range(2):
print ("Begin listening to %s. %s" %(func,ctime()))
sleep(4)
print("end listening %s"%ctime()) def move(func):
for i in range(2):
print ("Begin watching at the %s! %s" %(func,ctime()))
sleep(5)
print('end watching %s'%ctime()) threads = []
t1 = threading.Thread(target=music,args=('七里香',))
threads.append(t1)
t2 = threading.Thread(target=move,args=('阿甘正传',))
threads.append(t2) if __name__ == '__main__': for t in threads:
t.start()
t1.join() #当t1调用join()时 print ("all over %s" %ctime())
结果:
Begin listening to 七里香. Thu Sep 29 15:35:35 2016 #t1和t2启动
Begin watching at the 阿甘正传! Thu Sep 29 15:35:35 2016
end listening Thu Sep 29 15:35:39 2016
Begin listening to 七里香. Thu Sep 29 15:35:39 2016
end watching Thu Sep 29 15:35:40 2016
Begin watching at the 阿甘正传! Thu Sep 29 15:35:40 2016
end listening Thu Sep 29 15:35:43 2016 #t1结束,主线程继续往下执行
all over Thu Sep 29 15:35:43 2016 #主线程结束
end watching Thu Sep 29 15:35:45 2016 #t2结束
八、setDaemon(True)
将线程声明为守护线程,必须在start() 方法调用之前设置, 如果不设置为守护线程程序会被无限挂起。这个方法基本和join是相反的。当我们 在程序运行中,执行一个主线程,如果主线程又创建一个子线程,主线程和子线程 就兵分两路,分别运行,那么当主线程完成想退出时,会检验子线程是否完成。如 果子线程未完成,则主线程会等待子线程完成后再退出。但是有时候我们需要的是 只要主线程完成了,不管子线程是否完成,都要和主线程一起退出,这时就可以用setDaemon方法。
实例:
import threading
from time import ctime,sleep
import time def music(func):
for i in range(2):
print ("Begin listening to %s. %s" %(func,ctime()))
sleep(4)
print("end listening %s"%ctime()) def move(func):
for i in range(2):
print ("Begin watching at the %s! %s" %(func,ctime()))
sleep(5)
print('end watching %s'%ctime()) threads = []
t1 = threading.Thread(target=music,args=('七里香',))
threads.append(t1)
t2 = threading.Thread(target=move,args=('阿甘正传',))
threads.append(t2) if __name__ == '__main__': for t in threads:
t.setDaemon(True)
t.start() print ("all over %s" %ctime())
结果:
Begin listening to 七里香. Thu Sep 29 15:45:32 2016 #t1和t2启动,分别打印一次后sleep,主进程继续
Begin watching at the 阿甘正传! Thu Sep 29 15:45:32 2016
all over Thu Sep 29 15:45:32 2016 #主进程结束,程序结束
九、current_thread()
获取当前进程的名称。
十、同步锁
import time
import threading def addNum():
global num #在每个线程中都获取这个全局变量
# num-= temp=num
print('--get num:',num )
#time.sleep(0.1)
num =temp- #对此公共变量进行-1操作 num = #设定一个共享变量
thread_list = []
for i in range():
t = threading.Thread(target=addNum)
t.start()
thread_list.append(t) for t in thread_list: #等待所有线程执行完毕
t.join() print('final num:', num )
用num -= 1则最终结果没问题,这是因为完成这个操作太快了,在线程切换时间内。用中间变量temp进行赋值时出现问题,这是因为100个线程,每一个都没有执行完就就行了切换,因此最终得到的不是0。
多个线程同时操作同一个共享资源,所以导致冲突,这种情况就需要用同步锁来解决。
import time
import threading def addNum():
global num #在每个线程中都获取这个全局变量
# num-=
lock.acquire() #加同步锁
temp=num
print('--get num:',num )
#time.sleep(0.1)
num =temp- #对此公共变量进行-1操作
lock.release() #解锁 num = #设定一个共享变量
thread_list = []
lock=threading.Lock() #创建lock对象 for i in range():
t = threading.Thread(target=addNum)
t.start()
thread_list.append(t) for t in thread_list: #等待所有线程执行完毕
t.join() #所有线程执行完后主程序才能结束 print('final num:', num )
GIL与同步锁的作用对比:
GIL:同一时刻只能有一个线程进入解释器。
同步锁:同一时刻,保证只有一个线程被执行,在局部保证操作共享资源时不会发生冲突。
十一、线程死锁和递归锁
所谓死锁: 是指两个或两个以上的进程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用,它们都将无法推进下去。此时称系统处于死锁状态或系统产生了死锁,这些永远在互相等待的进程称为死锁进程。 由于资源占用是互斥的,当某个进程提出申请资源后,使得有关进程在无外力协助下,永远分配不到必需的资源而无法继续运行,这就产生了一种特殊现象死锁。
实例:
import threading,time class myThread(threading.Thread):
def doA(self):
lockA.acquire()
print(self.name,"gotlockA",time.ctime())
time.sleep()
lockB.acquire()
print(self.name,"gotlockB",time.ctime())
lockB.release()
lockA.release() def doB(self):
lockB.acquire()
print(self.name,"gotlockB",time.ctime())
time.sleep()
lockA.acquire()
print(self.name,"gotlockA",time.ctime())
lockA.release()
lockB.release()
def run(self):
self.doA()
self.doB()
if __name__=="__main__": lockA=threading.Lock()
lockB=threading.Lock()
threads=[]
for i in range():
threads.append(myThread())
for t in threads:
t.start()
for t in threads:
t.join()#等待线程结束,后面再讲。
死锁解决办法:
使用递归锁,创建Rlock对象,在需要加锁时使用
lockA=threading.Lock()
lockB=threading.Lock() lock = threading.Rlock()
python——threading模块的更多相关文章
- Python学习笔记- Python threading模块
Python threading模块 直接调用 # !/usr/bin/env python # -*- coding:utf-8 -*- import threading import time d ...
- python threading模块使用 以及python多线程操作的实践(使用Queue队列模块)
今天花了近乎一天的时间研究python关于多线程的问题,查看了大量源码 自己也实践了一个生产消费者模型,所以把一天的收获总结一下. 由于GIL(Global Interpreter Lock)锁的关系 ...
- python threading模块中对于信号的抓取
最近的物联网智能网关(树莓派)项目中遇到这样一个问题:要从多个底层串口读取发来的数据,并且做出相应的处理,对于每个串口的数据的读取我能想到的可以采用两种方式: 一种是采用轮询串口的方式,例如每3s向每 ...
- Python——threading模块(线程)
一.threading模块的对象 Thread:表示一个执行线程的对象 Lock:锁 Rlock:可重入锁对象 Condition:条件变量对象,使得一个线程等待另一个线程满足特定的“条件” Even ...
- python threading模块2
Thread 是threading模块中最重要的类之一,可以使用它来创建线程.有两种方式来创建线程:一种是通过继承Thread类,重写它的run方法:另一种是创建一个threading.Thread对 ...
- python threading 模块来实现多线程
以多线程的方式向标准输出打印日志 #!/usr/bin/python import time import threading class PrintThread(threading.Thread): ...
- python threading模块中的join()方法和setDeamon()方法的一些理解
之前用多线程的时候看见了很多文章,比较常用的大概就是join()和setDeamon()了. 先说一下自己对join()的理解吧: def join(self, timeout=None): &quo ...
- python threading模块
#coding=utf-8 import threading from time import ctime,sleep def music(func): for i in range(2): prin ...
- python threading模块的Lock和RLock区别
首先了解这两者是什么. 以下说明参考自python官网 Lock:Lock被称为①原始锁,原始锁是一个②在锁定时不属于特定线程的同步基元组件,它是能用的最低级的同步基元组件.原始锁处于 "锁 ...
随机推荐
- GZipStream 压缩和解压
GZipSteam: GZip 数据格式,它使用无损压缩和解压缩文件的行业标准算法 类 GZipStream有两种模式:CompressionMode.Compress和CompressionMode ...
- Mybatis 源码分析--crud
增加源码分析-insert() --------------------------------------------------------------------- public int ins ...
- 为什么要学习java?
前面说了什么是java java只是一门语言,中文,英语,c++,c#等等数之不尽的语言 java的应用领域: 1)安卓应用 2)金融业服务器的应用 3)Java Web应用 4)软件工具 5)交易应 ...
- 2016年Q2《网络安全创新500强》榜单解读
近日,美国投资咨询机构Cybersecurity Ventures发布了2016 Q2<网络安全创新500强>企业榜单,新兴安全公司root9B异军突起,国内4家企业上榜. 关于Cyber ...
- MySQL数据库4 - 查看数据表
一. 查看表的基本结构 语法:DESCRIBE/DESC TABLE_NAME 查询结果含义: Field: 字段名 Type: 字段类型 Null: 是否可以为空 Key: 是否编制索引 defau ...
- 重载运算符:类成员函数or友元函数
类成员函数: bool operator ==(const point &a)const { return x==a.x; } 友元函数: friend bool operator ==(co ...
- 一、Android屏幕的计量单位
px :是屏幕的像素点in :英寸mm:毫米pt :磅,1/72英寸dp :一个基于density的抽象单位,如果一个160dpi的屏幕,1dp=1pxdip :等同于dpsp :同dp ...
- 克隆虚拟机重启服务时 Error:No suitable device found: no device found for connection "System eth0"
故障说明: 在克隆几台虚拟机,发现启动后不能配置IP地址等信息,使用linux命令: “ifup eth0”也不能激活网卡, 而在使用"service network restart&quo ...
- pointer to function
指针.函数.数字.结构体.指针函数.函数指针 初学不好区分,做点儿实验来有效区分一下,以下代码采用dev-C++平台测试 //pointer to fucntion 函数功能是 基地址加偏移量得到偏移 ...
- NSOperation基本概念
NSOperation的作用 配合使用NSOperation和NSOperationQueue也能实现多线程编程 NSOperation和NSOperationQueue实现多线程的具体步骤 先将 ...