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接收传入函数的参数,以元组()的形式表示。

1 import threading
2 
3 def foo(n)
4     print("foo%s"%n)
5 t1 = threading.Thread(target=foo,args=(1,))  #创建线程对象

六、启动线程

通过线程对象t1.start()或t2.start()启动线程。

1 t1 = threading.Thread(target=sayhi, args=(1,))  # 生成一个线程实例
2 t2 = threading.Thread(target=sayhi, args=(2,))  # 生成另一个线程实例
3 
4 t1.start()  # 启动线程
5 t2.start()  # 启动另一个线程

 

实例1:

 1 import threading
 2 import time
 3 
 4 def foo(n):
 5     print("foo%s"%n)
 6     time.sleep(1)
 7 
 8 def bar(n):
 9     print("bar%s"%n)
10     time.sleep(2)
11 
12 t1 = threading.Thread(target=foo,args=(1,))
13 t2 = threading.Thread(target=bar,args=(2,))
14 
15 t1.start()
16 t2.start()
17 
18 print("...in the main...")
View Code

结果:

程序启动后,主线程从上到下依次执行,t1、t2两个子线程启动后,与主线程并行,抢占CPU资源。因此,前三行的输出结果几乎同时打印,没有先后顺序,此时,需要等t1和t2都结束后程序才结束。故等待2s后,程序结束。程序总共花了2s。

foo1
bar2
...in the main...

Process finished with exit code 0

实例2:

 1 import threading
 2 from time import ctime,sleep
 3 import time
 4 
 5 def music(func):
 6     for i in range(2):
 7         print ("Begin listening to %s. %s" %(func,ctime()))
 8         sleep(4)
 9         print("end listening %s"%ctime())
10 
11 def move(func):
12     for i in range(2):
13         print ("Begin watching at the %s! %s" %(func,ctime()))
14         sleep(5)
15         print('end watching %s'%ctime())
16 
17 threads = []
18 t1 = threading.Thread(target=music,args=('七里香',))
19 threads.append(t1)
20 t2 = threading.Thread(target=move,args=('阿甘正传',))
21 threads.append(t2)
22 
23 if __name__ == '__main__':
24 
25     for t in threads:
26         t.start()
27 
28     print ("all over %s" %ctime())
View Code

结果:

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:

 1 import threading
 2 from time import ctime,sleep
 3 import time
 4 
 5 def music(func):
 6     for i in range(2):
 7         print ("Begin listening to %s. %s" %(func,ctime()))
 8         sleep(4)
 9         print("end listening %s"%ctime())
10 
11 def move(func):
12     for i in range(2):
13         print ("Begin watching at the %s! %s" %(func,ctime()))
14         sleep(5)
15         print('end watching %s'%ctime())
16 
17 threads = []
18 t1 = threading.Thread(target=music,args=('七里香',))
19 threads.append(t1)
20 t2 = threading.Thread(target=move,args=('阿甘正传',))
21 threads.append(t2)
22 
23 if __name__ == '__main__':
24 
25     for t in threads:
26         t.start()
27         t.join()
28 
29     print ("all over %s" %ctime())
View Code

结果解析:

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:

 1 import threading
 2 from time import ctime,sleep
 3 import time
 4 
 5 def music(func):
 6     for i in range(2):
 7         print ("Begin listening to %s. %s" %(func,ctime()))
 8         sleep(4)
 9         print("end listening %s"%ctime())
10 
11 def move(func):
12     for i in range(2):
13         print ("Begin watching at the %s! %s" %(func,ctime()))
14         sleep(5)
15         print('end watching %s'%ctime())
16 
17 threads = []
18 t1 = threading.Thread(target=music,args=('七里香',))
19 threads.append(t1)
20 t2 = threading.Thread(target=move,args=('阿甘正传',))
21 threads.append(t2)
22 
23 if __name__ == '__main__':
24 
25     for t in threads:
26         t.start()
27     t.join()        #for循环的最后一次t的值,相当于t2
28 
29     print ("all over %s" %ctime())
View Code

结果:

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:

 1 import threading
 2 from time import ctime,sleep
 3 import time
 4 
 5 def music(func):
 6     for i in range(2):
 7         print ("Begin listening to %s. %s" %(func,ctime()))
 8         sleep(4)
 9         print("end listening %s"%ctime())
10 
11 def move(func):
12     for i in range(2):
13         print ("Begin watching at the %s! %s" %(func,ctime()))
14         sleep(5)
15         print('end watching %s'%ctime())
16 
17 threads = []
18 t1 = threading.Thread(target=music,args=('七里香',))
19 threads.append(t1)
20 t2 = threading.Thread(target=move,args=('阿甘正传',))
21 threads.append(t2)
22 
23 if __name__ == '__main__':
24 
25     for t in threads:
26         t.start()
27     t1.join()        #当t1调用join()时
28 
29     print ("all over %s" %ctime())
View Code

结果:

1 Begin listening to 七里香. Thu Sep 29 15:35:35 2016    #t1和t2启动
2 Begin watching at the 阿甘正传! Thu Sep 29 15:35:35 2016
3 end listening Thu Sep 29 15:35:39 2016
4 Begin listening to 七里香. Thu Sep 29 15:35:39 2016
5 end watching Thu Sep 29 15:35:40 2016
6 Begin watching at the 阿甘正传! Thu Sep 29 15:35:40 2016
7 end listening Thu Sep 29 15:35:43 2016    #t1结束,主线程继续往下执行
8 all over Thu Sep 29 15:35:43 2016      #主线程结束
9 end watching Thu Sep 29 15:35:45 2016    #t2结束

八、setDaemon(True)

将线程声明为守护线程,必须在start() 方法调用之前设置, 如果不设置为守护线程程序会被无限挂起。这个方法基本和join是相反的。当我们 在程序运行中,执行一个主线程,如果主线程又创建一个子线程,主线程和子线程 就兵分两路,分别运行,那么当主线程完成想退出时,会检验子线程是否完成。如 果子线程未完成,则主线程会等待子线程完成后再退出。但是有时候我们需要的是 只要主线程完成了,不管子线程是否完成,都要和主线程一起退出,这时就可以用setDaemon方法。

实例:

 1 import threading
 2 from time import ctime,sleep
 3 import time
 4 
 5 def music(func):
 6     for i in range(2):
 7         print ("Begin listening to %s. %s" %(func,ctime()))
 8         sleep(4)
 9         print("end listening %s"%ctime())
10 
11 def move(func):
12     for i in range(2):
13         print ("Begin watching at the %s! %s" %(func,ctime()))
14         sleep(5)
15         print('end watching %s'%ctime())
16 
17 threads = []
18 t1 = threading.Thread(target=music,args=('七里香',))
19 threads.append(t1)
20 t2 = threading.Thread(target=move,args=('阿甘正传',))
21 threads.append(t2)
22 
23 if __name__ == '__main__':
24 
25     for t in threads:
26         t.setDaemon(True)
27         t.start()
28 
29 
30     print ("all over %s" %ctime())
View Code

结果:

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()

获取当前进程的名称。

 

十、同步锁

 1 import time
 2 import threading
 3 
 4 def addNum():
 5     global num #在每个线程中都获取这个全局变量
 6     # num-=1
 7 
 8     temp=num
 9     print('--get num:',num )
10     #time.sleep(0.1)
11     num =temp-1 #对此公共变量进行-1操作
12 
13 
14 num = 100  #设定一个共享变量
15 thread_list = []
16 for i in range(100):
17     t = threading.Thread(target=addNum)
18     t.start()
19     thread_list.append(t)
20 
21 for t in thread_list: #等待所有线程执行完毕
22     t.join()
23 
24 print('final num:', num )

用num -= 1则最终结果没问题,这是因为完成这个操作太快了,在线程切换时间内。用中间变量temp进行赋值时出现问题,这是因为100个线程,每一个都没有执行完就就行了切换,因此最终得到的不是0。

多个线程同时操作同一个共享资源,所以导致冲突,这种情况就需要用同步锁来解决。

 1 import time
 2 import threading
 3 
 4 def addNum():
 5     global num #在每个线程中都获取这个全局变量
 6     # num-=1
 7     lock.acquire()    #加同步锁
 8     temp=num
 9     print('--get num:',num )
10     #time.sleep(0.1)
11     num =temp-1 #对此公共变量进行-1操作
12     lock.release()    #解锁
13 
14 num = 100  #设定一个共享变量
15 thread_list = []
16 lock=threading.Lock()  #创建lock对象
17 
18 for i in range(100):
19     t = threading.Thread(target=addNum)
20     t.start()
21     thread_list.append(t)
22 
23 for t in thread_list: #等待所有线程执行完毕
24     t.join()      #所有线程执行完后主程序才能结束
25 
26 print('final num:', num )

GIL与同步锁的作用对比:

GIL:同一时刻只能有一个线程进入解释器。

同步锁:同一时刻,保证只有一个线程被执行,在局部保证操作共享资源时不会发生冲突。

 

十一、线程死锁和递归锁

所谓死锁: 是指两个或两个以上的进程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用,它们都将无法推进下去。此时称系统处于死锁状态或系统产生了死锁,这些永远在互相等待的进程称为死锁进程。 由于资源占用是互斥的,当某个进程提出申请资源后,使得有关进程在无外力协助下,永远分配不到必需的资源而无法继续运行,这就产生了一种特殊现象死锁。

实例:

 1 import threading,time
 2 
 3 class myThread(threading.Thread):
 4     def doA(self):
 5         lockA.acquire()
 6         print(self.name,"gotlockA",time.ctime())
 7         time.sleep(3)
 8         lockB.acquire()
 9         print(self.name,"gotlockB",time.ctime())
10         lockB.release()
11         lockA.release()
12 
13     def doB(self):
14         lockB.acquire()
15         print(self.name,"gotlockB",time.ctime())
16         time.sleep(2)
17         lockA.acquire()
18         print(self.name,"gotlockA",time.ctime())
19         lockA.release()
20         lockB.release()
21     def run(self):
22         self.doA()
23         self.doB()
24 if __name__=="__main__":
25 
26     lockA=threading.Lock()
27     lockB=threading.Lock()
28     threads=[]
29     for i in range(5):
30         threads.append(myThread())
31     for t in threads:
32         t.start()
33     for t in threads:
34         t.join()#等待线程结束,后面再讲。
View Code

死锁解决办法:

使用递归锁,创建Rlock对象,在需要加锁时使用

1 lockA=threading.Lock()
2 lockB=threading.Lock()


lock = threading.Rlock()

 

posted on 2016-09-29 15:49  oliver.lee  阅读(9550)  评论(1编辑  收藏  举报

导航