[英]I don't see why this code doesn't work (multiprocessing.Pipe)
I'm studying Python multiprocessing Pipe. 我正在研究Python多处理管道。 My goal is two make two independent processes, one of which sends the other a message for five time.
我的目标是两个使两个独立的过程,其中一个发送另一个消息五次。 I have no problem running it, but it just shows their PIDs and that's it.
我运行它没有问题,但是仅显示其PID而已。 What did I wrong with this code?
这段代码有什么错? My environment is Windows 10(64bit) and Python 3.6.1(32bit).
我的环境是Windows 10(64位)和Python 3.6.1(32位)。
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()
It seems you forget to call run()
method of receiver
class (CHILD) which inherits the multiprocessing.Process
class (PARENT). 似乎您忘记了调用继承了
multiprocessing.Process
类(PARENT)的receiver
类(CHILD)的run()
方法。
Since run()
is not called explicitly, run()
method of parent is called and it doesn't have your receiving value printing code. 由于
run()
不显式调用, run()
母公司的方法被调用,它不会有你的接收值打印代码。 Therefore, it is giving feeling that code is not running. 因此,让人感觉代码没有运行。
Also some more things : 还有一些更多的东西:
run()
method need to called till the sending process is alive. run()
方法需要被调用,直到发送过程生效为止。 Please check the below code with above points incorporated. 请检查以下代码并结合以上几点。
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()
Output: 输出:
('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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.