Python Threading 线程模块用法
一、什么是 Threading
Threading用于提供线程相关的操作,线程是应用程序中工作的最小单元。python当前版本的多线程库没有实现优先级、线程组,线程也不能被停止、暂停、恢复、中断。
1.1、线程池图解
二、创建线程
导入模块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-=1
temp=num
print('--get num:',num )
#time.sleep(0.1)
num =temp-1 #对此公共变量进行-1操作
num = 100 #设定一个共享变量
thread_list = []
for i in range(100):
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-=1
lock.acquire() #加同步锁
temp=num
print('--get num:',num )
#time.sleep(0.1)
num =temp-1 #对此公共变量进行-1操作
lock.release() #解锁
num = 100 #设定一个共享变量
thread_list = []
lock=threading.Lock() #创建lock对象
for i in range(100):
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(3)
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(2)
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(5):
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()