# Python多线程与多进程浅析之二

+关注继续查看

### Python 多线程 Step by Step

Python 在 CPU 密集运算的场景，多个线程并不能提高太多性能，而对于 I/O 阻塞的场景，可以使得运行效率获得几倍的提高。我们接下来会详细的分析一下。

# 计算一次 my_cal() 函数，获得耗用时间
>>> from time import time
>>> def my_cal(a):
...     j = 0
...     for i in range(a):
...         j = j + i
...     print(j)
...     return j

>>> start = time()
>>> my_cal(1000000)
>>> end = time()
>>> print('cost time {:f} s'.format(end - start))
499999500000
cost time 0.106146 s

>>> from time import time

>>> def my_cal(a):
...     j = 0
...     for i in range(a):
...         j = j + i
...     print(j)
...     return j

>>> start = time()
>>> list_01 = [1000000, 1500000, 2000000, 2500000, 3000000]

>>> for i in list_01:
...    my_cal(i)

>>> end = time()
>>> print('cost time {:f} s'.format(end - start))
499999500000
1124999250000
1999999000000
3124998750000
4499998500000
cost time 0.906054 s

>>> from time import time

>>> def my_cal(a):
...     j = 0
...     for i in range(a):
...         j = j + i
...     print(j)
...     return j

>>> start = time()

>>> list_01 = [1000000, 1500000, 2000000, 2500000, 3000000]
>>> list_02 = list(map(my_cal, list_01))

>>> end = time()
>>> print('cost time {:f} s'.format(end - start))
499999500000
1124999250000
1999999000000
3124998750000
4499998500000
cost time 0.927783 s

>>> from time import time

>>> def my_cal(a):
...     j = 0
...     for i in range(a):
...         j = j + i
...     print(j)
...     return j

>>> start = time()
>>> list_01 = [1000000, 1500000, 2000000, 2500000, 3000000]

>>> for i in list_01:
...     # 建立线程
...     # 建立线程列表

# 等待线程结束

>>> end = time()
>>> print('\ncost time {:f} s'.format(end - start))
499999500000
1124999250000
1999999000000
3124998750000
4499998500000

cost time 1.074110 s

thread 的 join() 方法阻塞当前线程，等待子线程执行完毕后自己再继续执行。如果没有 join() 的阻塞，我们会先看到 cost time 的计算，而实际上上面的线程还没有完成自己的任务。

# 检查线程的状态
>>> from time import time

>>> def my_cal(a):
...     j = 0
...     for i in range(a):
...         j = j + i
...     print(j)
...     return j

# 创建后的线程的状态
# 线程启动后的状态
4499998500000

### 多进程方式

# 多进程方式
>>> from time import time
>>> from concurrent.futures import *

>>> def my_cal(a):
...     j = 0
...     for i in range(a):
...         j = j + i
...     print(j)
...     return j

>>> list_01 = [1000000, 2000000, 1500000, 2500000, 3000000]
>>> start = time()
>>> pool = ProcessPoolExecutor(max_workers=10)
>>> list_02 = list(pool.map(my_cal, list_01))
>>> end = time()

>>> print('cost time {:f} s'.format(end - start))
499999500000
1124999250000
1999999000000
3124998750000
4499998500000
cost time 0.374396 s

# 多进程方式，测试 workers

from concurrent.futures import *

def my_cal(a):
j = 0
for i in range(a):
j = j + i
return j

def my_cal_main(workers):

list_01 = [1000000, 2000000, 1500000, 2500000, 3000000]
pool = ProcessPoolExecutor(workers)
list_02 = list(pool.map(my_cal, list_01))

for i in range(2,11):
print(i)
%timeit -n6 my_cal_main(i)

# 输出结果
2
6 loops, best of 3: 491 ms per loop
3
6 loops, best of 3: 443 ms per loop
4
6 loops, best of 3: 437 ms per loop
5
6 loops, best of 3: 352 ms per loop
6
6 loops, best of 3: 336 ms per loop
7
6 loops, best of 3: 341 ms per loop
8
6 loops, best of 3: 346 ms per loop
9
6 loops, best of 3: 346 ms per loop
10
6 loops, best of 3: 347 ms per loop

# 获得 cpu 核心数量
>>> import multiprocessing
>>> pool = multiprocessing.Pool()
>>> print(pool._processes)
8
>>> print(multiprocessing.cpu_count())
8

>>> import psutil
>>> psutil.cpu_count()
8

java多线程 -- Condition 控制线程通信
Api文档如此定义： Condition 将 Object 监视器方法（wait、notify 和 notifyAll）分解成截然不同的对象，以便通过将这些对象与任意 Lock 实现组合使用，为每个对象提供多个等待 set（wait-set）。
692 0
python多线程，so easy

704 0

1318 0
python3,进程间的通信

866 0
+关注
11

0

《2021云上架构与运维峰会演讲合集》

《零基础CSS入门教程》

《零基础HTML入门教程》