2017-10-12 13 views
0

J'étudie Python multitraitement Pipe. Mon but est de faire deux processus indépendants, l'un d'entre eux envoie un message à l'autre cinq fois. Je n'ai aucun problème à l'exécuter, mais il montre juste leurs PIDs et c'est tout. Qu'est-ce que j'ai eu de mal avec ce code? Mon environnement est Windows 10 (64 bits) et Python 3.6.1 (32 bits).Je ne vois pas pourquoi ce code ne fonctionne pas (multiprocessing.Pipe)

import os 
import multiprocessing as mp 
import time 

global sending_end, receiving_end 
sending_end, receiving_end = mp.Pipe() 

def sender(sending_end=sending_end): 
    print('SND PID: ', os.getpid()) 
    for _ in range(5): 
     sending_end.send('test') 
     time.sleep(1) 


class receiver(mp.Process): 
    def __init__(self): 
     mp.Process.__init__(self) 

    def run(self, receiving_end=receiving_end): 
     print('REC PID: ', os.getpid()) 
     print(receiving_end.recv()) 
     time.sleep(1) 


if __name__ == '__main__': 

    print('MAIN PID: ', os.getpid()) 

    s = mp.Process(target = sender, args=(sending_end,)) 
    s.start() 

    r = receiver() 
    r.start()  

    mp.freeze_support() 
+0

Pourquoi les tuyaux d'utilisation? Il y a une [Queue] (https://docs.python.org/3.3/library/multiprocessing.html?highlight=multiprocessing#exchanging-objects-between-processes), qui fait tout pour vous. – uphill

+0

@uphill Je pense que l'utilisation de tuyaux sera plus compatible au cas où il y aurait des chances d'échanger des messages bidirectionnellement entre les processus. – maynull

+0

si vous n'utilisez pas python comme autre processus, vous pourriez envisager [subprocess] (https://docs.python.org/3/library/subprocess.html?highlight=subprocess#module-subprocess), puisque le mutliprocessing est une goutte en remplacement d'utiliser plusieurs cpu en évitant le GIL, en engendrant plus de processus python. – uphill

Répondre

0

Il vous semble oublier d'appeler run() méthode de receiver classe (enfant) qui hérite de la classe multiprocessing.Process (PARENT).

Depuis run() n'est pas appelée explicitement, run() méthode de parent est appelée et il n'a pas votre code d'impression de valeur de réception. Par conséquent, il donne l'impression que le code ne fonctionne pas.

aussi quelques autres choses:

  • Les deux tuyaux doivent être fermées à la fin comme vous fermez le fichier.
  • La classe enfant run() doit être appelée jusqu'à ce que le processus d'envoi soit actif.

Veuillez vérifier le code ci-dessous avec les points ci-dessus incorporés.

code:

import os 
import multiprocessing as mp 
import time 

global sending_end, receiving_end 
sending_end, receiving_end = mp.Pipe() 


def sender(sending_end=sending_end): 
    print('SND PID: ', os.getpid()) 
    for i in range(5): 
     sending_end.send('test_' + str(i)) 
     time.sleep(1) 
    print "Done from sender" 
    #Closing sending pipe 
    sending_end.close() 


class receiver(mp.Process): 
    def __init__(self): 
     mp.Process.__init__(self) 

    def run(self, receiving_end=receiving_end): 
     print('REC PID: ', os.getpid()) 
     print("Dinesh - ",receiving_end.recv()) 
     time.sleep(1) 


if __name__ == '__main__': 
    import sys 
    print('MAIN PID: ', os.getpid()) 

    s = mp.Process(target = sender, args=(sending_end,)) 
    s.start() 

    r = receiver() 
    r.start() 

    while True: 
     #Checking sending process is alive or not 
     if not s.is_alive(): 
      print "Sending process is done. Exiting" 
      #Closing receiving end pipe 
      receiving_end.close() 
      #Closing receving process 
      r.terminate() 
      sys.exit() 
     time.sleep(0.1) 
     #Explicitly calling run method 
     r.run() 

    mp.freeze_support() 

sortie:

('MAIN PID: ', 16400) 
('REC PID: ', 16400) 
('REC PID: ', 12544) 
('SND PID: ', 17744) 
('Dinesh - ', 'test_0') 
('REC PID: ', 16400) 
('Dinesh - ', 'test_1') 
('REC PID: ', 16400) 
('Dinesh - ', 'test_2') 
('REC PID: ', 16400) 
('Dinesh - ', 'test_3') 
('REC PID: ', 16400) 
('Dinesh - ', 'test_4') 
Done from sender 
Sending process is done. Exiting