Python线程并发详细操作教程
多重处理 | 多重编程 |
多处理是指由多个CPU同时处理多个进程。 | 多重编程可将多个程序同时保存在主存储器中,并使用单个CPU同时执行它们。 |
它使用多个CPU。 | 它使用单个CPU。 |
它允许并行处理。 | 发生上下文切换。 |
处理工作所需的时间更少。 | 更多的时间来处理作业。 |
它促进了计算机系统设备的高效利用。 | 效率低于多重处理。 |
通常更贵。 | 这种系统便宜些。 |
通过多处理,我们利用了多个进程的功能,因此,我们利用了GIL的多个实例。
因此,在任何时候都没有限制在我们的程序中执行一个线程的字节码。
Fork
Spawn
Forkserver
fork()-这是一个通常在内核中实现的系统调用。它用于创建流程的副本。p>
getpid()-此系统调用返回调用进程的进程ID(PID)。
# Filename : example.py
# Copyright : 2020 By Bianchenghao6
# Author by : bianchenghao6.com
# Date : 2020-08-22
import os
def child():
n = os.fork()
if n > 0:
print("PID of Parent process is : ", os.getpid())
else:
print("PID of Child process is : ", os.getpid())
child()
# Filename : example.py
# Copyright : 2020 By Bianchenghao6
# Author by : bianchenghao6.com
# Date : 2020-08-22
PID of Parent process is : 25989
PID of Child process is : 25990
导入多处理模块。
创建对象过程。
通过调用 start()方法来启动流程活动。
等待直到过程完成工作并通过调用 join()方法退出。
# Filename : example.py
# Copyright : 2020 By Bianchenghao6
# Author by : bianchenghao6.com
# Date : 2020-08-22
import multiprocessing
def spawn_process(i):
print ('This is process: %s' %i)
return
if __name__ == '__main__':
Process_jobs = []
for i in range(3):
p = multiprocessing.Process(target = spawn_process, args = (i,))
Process_jobs.append(p)
p.start()
p.join()
# Filename : example.py
# Copyright : 2020 By Bianchenghao6
# Author by : bianchenghao6.com
# Date : 2020-08-22
This is process: 0
This is process: 1
This is process: 2
使用Forkserver机制实例化服务器以启动新进程。
然后服务器接收命令并处理所有创建新进程的请求。
对于创建新进程,我们的python程序将向Forkserver发送请求,并将为我们创建一个进程。
最后,我们可以在程序中使用这个新创建的过程。
# Filename : example.py
# Copyright : 2020 By Bianchenghao6
# Author by : bianchenghao6.com
# Date : 2020-08-22
import multiprocessing
import time
def nondaemonProcess():
print("starting my Process")
time.sleep(8)
print("ending my Process")
def daemonProcess():
while True:
print("Hello")
time.sleep(2)
if __name__ == '__main__':
nondaemonProcess = multiprocessing.Process(target = nondaemonProcess)
daemonProcess = multiprocessing.Process(target = daemonProcess)
daemonProcess.daemon = True
nondaemonProcess.daemon = False
daemonProcess.start()
nondaemonProcess.start()
# Filename : example.py
# Copyright : 2020 By Bianchenghao6
# Author by : bianchenghao6.com
# Date : 2020-08-22
starting my Process
ending my Process
# Filename : example.py
# Copyright : 2020 By Bianchenghao6
# Author by : bianchenghao6.com
# Date : 2020-08-22
import multiprocessing
import time
def Child_process():
print ('Starting function')
time.sleep(5)
print ('Finished function')
P = multiprocessing.Process(target = Child_process)
P.start()
print("My Process has terminated, terminating main thread")
print("Terminating Child Process")
P.terminate()
print("Child Process successfully terminated")
# Filename : example.py
# Copyright : 2020 By Bianchenghao6
# Author by : bianchenghao6.com
# Date : 2020-08-22
My Process has terminated, terminating main thread
Terminating Child Process
Child Process successfully terminated
# Filename : example.py
# Copyright : 2020 By Bianchenghao6
# Author by : bianchenghao6.com
# Date : 2020-08-22
import multiprocessing
print(multiprocessing.current_process().pid)
# Filename : example.py
# Copyright : 2020 By Bianchenghao6
# Author by : bianchenghao6.com
# Date : 2020-08-22
import multiprocessing
import time
def Child_process():
print("PID of Child Process is: {}".format(multiprocessing.current_process().pid))
print("PID of Main process is: {}".format(multiprocessing.current_process().pid))
P = multiprocessing.Process(target=Child_process)
P.start()
P.join()
# Filename : example.py
# Copyright : 2020 By Bianchenghao6
# Author by : bianchenghao6.com
# Date : 2020-08-22
PID of Main process is: 9401
PID of Child Process is: 9402
我们需要定义 Process 类的新子类。
我们需要重写 _init_(self [,args])类。
我们需要重写 run(self [,args])方法的,以实现 Process
我们需要通过调用 start()方法来启动该过程。
# Filename : example.py
# Copyright : 2020 By Bianchenghao6
# Author by : bianchenghao6.com
# Date : 2020-08-22
import multiprocessing
class MyProcess(multiprocessing.Process):
def run(self):
print ('called run method in process: %s' %self.name)
return
if __name__ == '__main__':
jobs = []
for i in range(5):
P = MyProcess()
jobs.append(P)
P.start()
P.join()
# Filename : example.py
# Copyright : 2020 By Bianchenghao6
# Author by : bianchenghao6.com
# Date : 2020-08-22
called run method in process: MyProcess-1
called run method in process: MyProcess-2
called run method in process: MyProcess-3
called run method in process: MyProcess-4
called run method in process: MyProcess-5
# Filename : example.py
# Copyright : 2020 By Bianchenghao6
# Author by : bianchenghao6.com
# Date : 2020-08-22
def square(n):
result = n*n
return result
if __name__ == '__main__':
inputs = list(range(5))
p = multiprocessing.Pool(processes = 4)
p_outputs = pool.map(function_square, inputs)
p.close()
p.join()
print ('Pool :', p_outputs)
# Filename : example.py
# Copyright : 2020 By Bianchenghao6
# Author by : bianchenghao6.com
# Date : 2020-08-22
Pool : [0, 1, 4, 9, 16]