in fact ,Python Another important topic of multithreading is ,GIL
(Global Interpreter Lock
, Global interpreter lock ).
Multithreading is not necessarily faster than single threading
stay Python in , It can be done through multiple processes 、 Multithreading and multiprocessing to achieve multitasking . Can multithreading be faster than a single thread ?
Now I use a piece of code to prove my point of view .
'''
@Author: Runsen
@ WeChat official account : Python King
@ Blog : https://blog.csdn.net/weixin_44510615
@Date: 2020/6/4
'''
import threading, time
def my_counter():
i = 0
for _ in range(100000000):
i = i+1
return True
def main1():
start_time = time.time()
for tid in range(2):
t = threading.Thread(target=my_counter)
t.start()
t.join() # The first time around join Method causes the main thread to block , But the second thread doesn't start , So the two threads are executed sequentially
print(" Single thread sequential execution total_time: {}".format(time.time() - start_time))
def main2():
thread_ary = {}
start_time = time.time()
for tid in range(2):
t = threading.Thread(target=my_counter)
t.start()
thread_ary[tid] = t
for i in range(2):
thread_ary[i].join() # Both threads have started , So two threads are concurrent
print(" Multithreaded execution total_time: {}".format(time.time() - start_time))
if __name__ == "__main__":
main1()
main2()
Copy code
Running results
Single thread sequential execution total_time: 17.754502773284912
Multithreaded execution total_time: 20.01178550720215
Copy code
I'm afraid you'll say I'll make a mess of it , I'd like to take a picture and see it clearly
At this time , I doubt that : Is there something wrong with my machine ? It's not like that , In essence Python The thread of is invalid , It doesn't work in parallel .
Python The thread of , It does encapsulate the underlying operating system threads , stay Linux In the system is Pthread
( Its full name is POSIX Thread
), And in the Windows In the system is Windows Thread
. in addition ,Python The thread of , And it's completely managed by the operating system , For example, coordinating when to implement 、 Managing memory resources 、 Management interruption and so on .
GIL No Python Characteristics of
GIL In a simple sentence , Namely Any moment , No matter how many threads , A single CPython The interpreter can only execute one bytecode . This definition needs attention :
The first thing to be clear is GIL Not at all Python Characteristics of , It's about achieving Python Parser (CPython) A concept introduced by .
C++ It's a set of languages ( grammar ) standard , But different compilers can be used to compile executable code . Famous compilers such as GCC,INTEL C++,Visual C++ etc. .
Python It's the same thing , The same piece of code can be passed through CPython,PyPy,Psyco Such as different Python Execution environment to execute .
other Python The interpreter doesn't have to have GIL. for example Jython (JVM) and IronPython (CLR) No, GIL, and CPython,PyPy Yes GIL;
because CPython Is the default in most environments Python execution environment . So in many people's concepts CPython Namely Python, Take it for granted GIL It comes down to Python The flaw of language . So let's be clear here :GIL Not at all Python Characteristics of ,Python It can be completely independent of GIL
GIL The essence is a mutually exclusive lock
GIL The essence is a mutually exclusive lock , Since it's a mutex , The essence of all mutexes is the same , It's all about making concurrent runs serial , In this way, the shared data can only be modified by one task in the same time , To ensure data security .
One thing for sure is : Securing different data , We should add different locks .
GIL How it works : So let's take this picture , It's just one. GIL stay Python Working example of the program . among ,Thread 1、2、3 Take turns to execute , When each thread starts executing , Will lock up GIL, To prevent other threads from executing ; alike , After each thread finishes executing a section , Will release GIL, To allow other threads to start using resources .
Computationally intensive
Computing intensive tasks are characterized by a large number of calculations , Consume CPU resources .
Let's start with a simple computationally intensive example :
'''
@Author: Runsen
@ WeChat official account : Python King
@ Blog : https://blog.csdn.net/weixin_44510615
@Date: 2020/6/4
'''
import time
COUNT = 50_000_000
def count_down():
global COUNT
while COUNT > 0:
COUNT -= 1
s = time.perf_counter()
count_down()
c = time.perf_counter() - s
print('time taken in seconds - >:', c)
time taken in seconds - >: 9.2957003
Copy code
This is a single thread , Time is 9s, Let's use two threads to see what the results are :
'''
@Author: Runsen
@ WeChat official account : Python King
@ Blog : https://blog.csdn.net/weixin_44510615
@Date: 2020/6/4
'''
import time
from threading import Thread
COUNT = 50_000_000
def count_down():
global COUNT
while COUNT > 0:
COUNT -= 1
s = time.perf_counter()
t1 = Thread(target=count_down)
t2 = Thread(target=count_down)
t1.start()
t2.start()
t1.join()
t2.join()
c = time.perf_counter() - s
print('time taken in seconds - >:', c)
time taken in seconds - >: 17.110625
Copy code
The main operation of our program is to calculate , CPU No waiting , And after changing to multithreading , After adding threads , Frequent switching between threads , Increased time cost , Time will certainly increase .
Another type is IO intensive , Network involved 、 disk IO The tasks of are IO Intensive task , This kind of task is characterized by CPU Consume little , Most of the time the task is waiting IO Operation is completed ( because IO The speed is much lower than CPU And the speed of memory ). about IO Intensive task , The more tasks ,CPU The more efficient , But there is also a limit . Most common tasks are IO Intensive task , such as Web application .
summary : about io Intensive work (Python Reptiles ), Multithreading can greatly improve code efficiency . Yes CPU Computationally intensive (Python Data analysis , machine learning , Deep learning ), Multithreading may be slightly less efficient than a single thread . therefore , In the field of data, there is no saying that multithreading can improve efficiency , Only will CPU Upgrade to GPU,TPU To improve computing power .
author : Liu Runsen
link :https://juejin.im/post/6892747972933910535
source : Nuggets
The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source .