Python中的多进程与多线程详解
文章目录
一、多进程(Multiprocessing)
1.1 概念
1.2 创建多进程的方式
multiprocessing.Process
类
import multiprocessing
def worker():
print('This is a worker process')
if __name__ == '__main__':
p = multiprocessing.Process(target = worker)
p.start()
p.join()
worker
,然后通过创建 Process
类的实例,将 worker
函数作为目标函数。start
方法用于启动进程,join
方法用于等待进程结束。Pool
)
import multiprocessing
def square(x):
return x * x
if __name__ == '__main__':
with multiprocessing.Pool(processes = 4) as pool:
results = pool.map(square, [1, 2, 3, 4, 5])
print(results)
square
,然后使用进程池将该函数应用到一个列表中的每个元素上。1.3 进程间通信(IPC)
Queue
)
import multiprocessing
def writer(q):
for i in range(10):
q.put(i)
def reader(q):
while True:
try:
item = q.get()
print(item)
except multiprocessing.Queue.Empty:
break
if __name__ == '__main__':
q = multiprocessing.Queue()
pw = multiprocessing.Process(target = writer, args=(q,))
pr = multiprocessing.Process(target = reader, args=(q,))
pw.start()
pw.join()
pr.start()
pr.join()
Pipe
)
import multiprocessing
def f(conn):
conn.send([42, None, 'hello'])
conn.close()
if __name__ == '__main__':
parent_conn, child_conn = multiprocessing.Pipe()
p = multiprocessing.Process(target = f, args=(child_conn,))
p.start()
print(parent_conn.recv())
p.join()
1.4 优点
1.5 缺点
二、多线程(Multithreading)
2.1 概念
2.2 创建多线程的方式
threading.Thread
类
import threading
def worker():
print('This is a worker thread')
t = threading.Thread(target = worker)
t.start()
t.join()
worker
,通过创建 Thread
类的实例,将 worker
函数作为目标函数,然后启动和等待线程。2.3 线程同步机制
Lock
)
import threading
counter = 0
lock = threading.Lock()
def increment():
global counter
for _ in range(10000):
lock.acquire()
try:
counter += 1
finally:
lock.release()
t1 = threading.Thread(target = increment)
t2 = threading.Thread(target = increment)
t1.start()
t2.start()
t1.join()
t2.join()
print(counter)
counter
)时,需要使用锁来保证数据的正确性。lock.acquire()
用于获取锁,lock.release()
用于释放锁。Condition
)
import threading
condition = threading.Condition()
data = []
def producer():
global data
with condition:
for i in range(10):
data.append(i)
condition.notify()
def consumer():
global data
with condition:
while not data:
condition.wait()
item = data.pop()
print(item)
t1 = threading.Thread(target = producer)
t2 = threading.Thread(target = consumer)
t1.start()
t2.start()
t1.join()
t2.join()
2.4 优点
2.5 缺点
作者:Limiiiing