2017-10-12 146 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 
相關問題