2017-10-12 209 views
0

我正在学习Python多处理管道。我的目标是两个独立的进程,其中一个发送另一个消息五次。我运行它没有问题,但它只是显示它们的PID,就是这样。这段代码我错了什么?我的环境是Windows 10(64位)和Python 3.6.1(32位)。我不明白为什么这段代码不起作用(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

为什么要使用管道?有一个[队列](https://docs.python.org/3.3/library/multiprocessing.html?highlight=multiprocessing#exchanging-objects-between-processes),它为你做了所有的事情。 – uphill

+0

@uphill我认为使用管道将更加兼容,以防在进程间双向交换消息的机会。 – maynull

+0

如果你没有使用python作为其他进程,你可能会考虑[subprocess](https://docs.python.org/3/library/subprocess.html?highlight=subprocess#module-subprocess),因为mutliprocessing是一个drop通过产生更多的python进程来替代使用多个cpu来避免GIL。 – uphill

回答

0

看来你忘了打电话的receiver类(儿童)run()方法,它继承了multiprocessing.Process类(父)。

由于run()未明确调用,因此调用父项的方法run()并且它没有您的接收值打印代码。因此,它给人的感觉是代码没有运行。

也有一些更多的东西:

  • 无论是管道需要像你关闭该文件将在年底关闭。
  • 孩子类run()方法需要调用,直到发送过程还活着。

请检查以上代码并结合以上几点。

代码:

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() 

输出:

('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 
相关问题