""" Exemple de 2 processus qui tournent en parallèle et échangent une variable avec Pipe. https://docs.python.org/3/library/multiprocessing.html#exchanging-objects-between-processes The Pipe() function returns a pair of connection objects connected by a pipe which by default is duplex (two-way). """ import os from multiprocessing import Process, Pipe def test(n): """Fonction pour charger le processeur""" for i in range(n): m = (i ** 1.89999) ** 0.8974125 def first_processus(conn): print('first process:', os.getppid()) a = 0 while 1: test(1000000) a += 1 print("a =", a) b = conn.recv() print("b dans first_process =", b) a += b[1] def second_processus(conn): print('second process:', os.getppid()) b = 0 while 1: test(2000000) b += 5 print("b dans second process =", b) conn.send(['b', b]) def main(): """ https://docs.python.org/3/library/multiprocessing.html#multiprocessing.Pipe multiprocessing.Pipe([duplex]) If duplex is True (the default) then the pipe is bidirectional. If duplex is False then the pipe is unidirectional: conn1 can only be used for receiving messages and conn2 can only be used for sending messages. """ parent_conn, child_conn = Pipe() p = Process(target=second_processus, args=(child_conn,)) p.start() first_processus(parent_conn) if __name__ == '__main__': main()