python的多线程和多进程

CPython中存在GIL,即全局锁,同一进程的多个线程,不能在同一时间运行代码,而且由于多个线程切换会消耗cpu,因此多线程甚至会更慢

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#! /usr/bin/python
from threading import Thread
from multiprocessing import Process
import time
import sys
def my_counter():
i = 0
for _ in range(100000000):
i = i + 1
return True
def single_thread():
start_time = time.time()
for tid in range(3):
my_counter()
end_time = time.time()
print("Total time: {}".format(end_time - start_time))
def mutiple_thread():
thread_array = []
start_time = time.time()
for tid in range(3):
t = Thread(target=my_counter)
t.start()
thread_array.append(t)
for t in thread_array:
t.join()
end_time = time.time()
print("Total time: {}".format(end_time - start_time))
def mutiple_porcess():
process_array = []
start_time = time.time()
for pid in range(3):
p = Process(target=my_counter)
p.start()
process_array.append(p)
for p in process_array:
p.join()
end_time = time.time()
print("Total time: {}".format(end_time - start_time))
if __name__ == '__main__':
fun = sys.argv[1]
if fun == 's':
single_thread()
elif fun == 'mt':
mutiple_thread()
else:
mutiple_porcess()

运行结果:

1
2
3
4
5
6
HandeMacBook-Pro:MyTest HAN$ python process_thread.py s
Total time: 18.80652904510498
HandeMacBook-Pro:MyTest HAN$ python process_thread.py mt
Total time: 19.265798091888428
HandeMacBook-Pro:MyTest HAN$ python process_thread.py mp
Total time: 9.154397964477539

参考:

Python: what are the differences between the threading and multiprocessing modules?

Python并发编程之线程池/进程池