Python is a language that runs in an interpreter. Looking up information, I know that there is a global lock (GIL) in Python. When using multi-process (Thread), it cannot take advantage of multi-core. The use of multi-process (Multiprocess), you can take advantage of multi-core to really improve efficiency.
Comparative Experiment
According to the data, if the multi-threaded process is CPU-intensive , then multi-threading will not have much efficiency improvement. On the contrary, the efficiency may decrease due to frequent thread switching. Multi-process is recommended; if it is IO-intensive , Multi-threaded processes can use the idle time of IO blocking and waiting to execute other threads to improve efficiency. So we compare the efficiency of different scenarios based on experiments
operating system
CPU
RAM
hard disk
Windows 10
Dual-core
8GB
Mechanical hard disk
(1) Import the required modules
1
2
3
4
import requests
import time
from threading
import Thread
from multiprocessing
import Process
(2) Define CPU-intensive calculation functions
1
2
3
4
5
6
7
def count (x, y) :
# Make the program complete 500,000 calculations
c =
0
while c <
500000:
c +=
1
x += x
y += y
(3)定义IO密集的文件读写函数
1
2
3
4
5
6
7
8
9
10
defwrite():
f = open(
"test.txt",
"w")
for x
in range(
5000000):
f.write(
"testwrite\n")
f.close()
defread():
f = open(
"test.txt",
"r")
lines = f.readlines()
f.close()
(4) 定义网络请求函数
1
2
3
4
5
6
7
8
9
10
_head = {
'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36'}
(11) Test multi-process concurrent execution of Http request-intensive operations
t = time.time()
httprs = []
t = time.time()
for x in range(10):
process = Process(target=http_request)
ios.append(process)
process.start()
e = httprs.__len__()
whileTrue:
for th in httprs:
ifnot th.is_alive():
e -= 1if e <= 0:
break
print("Multiprocess Http Request", time.time() - t)
Output: 0.5329999923706055、0.4760000705718994
Experimental results
CPU-intensive operations
IO-intensive operations
Network request-intensive operations
Linear operation
94.91824996469
22.46199995279
7.3296000004
Multithreaded operation
101.1700000762
24.8605000973
0.5053332647
Multi-process operation
53.8899999857
12.7840000391
0.5045000315
Through the above results, we can see: >
Multi-threading does not seem to have a great advantage under IO-intensive operations (maybe even more heavier tasks for IO operations can show advantages). Under CPU-intensive operations, it is obviously worse than single-threaded linear execution. But for network requests such as busy and other blocked thread operations, the advantage of multithreading is very significant
Multi-processes can show performance advantages whether they are CPU-intensive or IO-intensive, and network request-intensive (operations that often occur thread blocking). However, in similar network request-intensive operations, it is not much different from multi-threading, but it takes up resources such as CPU, so in this case, we can choose multi-threaded execution