multiprocessing模块提供了多种进程间通信方式,以下是一些常用的方法:
1、Queue: 创建一个队列对象,用于在进程之间传递数据。可以设置队列的最大长度和阻塞时间等选项。
from multiprocessing import Process, Queuedef worker(q):q.put('hello')if __name__ == '__main__':q = Queue()p = Process(target=worker, args=(q,))p.start()p.join()print(q.get())
2、Pipe: 创建一个管道对象,用于在进程之间传递数据。可以设置管道的缓冲区大小等选项。
from multiprocessing import Process, Pipedef worker(conn):conn.send('hello')conn.close()if __name__ == '__main__':parent_conn, child_conn = Pipe()p = Process(target=worker, args=(child_conn,))p.start()p.join()print(parent_conn.recv())
3、Value和Array: 创建共享变量或数组对象,可以在进程之间共享数据。可以设置共享变量或数组的初始值、类型和大小等选项。
from multiprocessing import Process, Value, Array
import ctypesdef worker(n, a):n.value = 3.1415926for i in range(len(a)):a[i] = ctypes.c_double(a[i])if __name__ == '__main__':num = Value('d', 0.0)arr = Array('d', [1.0, 2.0, 3.0])p = Process(target=worker, args=(num, arr))p.start()p.join()print(num.value)print(arr[:])
4、Lock和RLock: 创建锁对象,用于保护共享资源的访问。可以设置锁的超时时间等选项。
from multiprocessing import Process, Lock, RLock
import timedef worker(lock, num):lock.acquire()try:time.sleep(1)finally:lock.release()num.value += 1if __name__ == '__main__':num = Value('i', 0)lock = Lock()p = Process(target=worker, args=(lock, num))p.start()p.join()print(num.value)
5、Event和Barrier: 创建事件对象和障碍对象,用于协调多个进程的执行。可以设置事件和障碍的等待时间等选项。
from multiprocessing import Process, Event, Barrier
import timedef worker(event, barrier):event.wait()print('Worker')barrier.wait()print('Barrier')if __name__ == '__main__':event = Event()barrier = Barrier(3)p1 = Process(target=worker, args=(event, barrier))p2 = Process(target=worker, args=(event, barrier))p3 = Process(target=worker, args=(event, barrier))p1.start()p2.start()p3.start()event.set()p1.join()p2.join()p3.join()