最近我一直在使用Pipe和树莓pi。我正试图向我的函数发送一个信号来杀死它;pipe.recv";正在阻止该功能。信号被发送,但是while循环没有被执行。
from multiprocessing import Process, Pipe
import time
import os
import signal
def start(pipe):
pipe1 = pipe[1].recv()
while True:
print('hello world')
os.kill(pipe1,signal.SIGTERM)
if __name__ == "__main__":
conn1 = Pipe()
a = Process(target = start,args = (conn1,))
a.start()
time.sleep(5)
print("TIMES UP")
conn1[1].send(a.pid)
您正在发送并试图从管道的同一端检索项。尝试这样做,其中pipe[0]
和pipe[1]
被命名为parent
和child
,以提高可读性,而不是:
from multiprocessing import Process, Pipe
import time
import os
import signal
def start(child):
pipe1 = child.recv()
while True:
print('hello world')
os.kill(pipe1,signal.SIGTERM)
if __name__ == "__main__":
parent, child = Pipe()
a = Process(target = start,args = (child,))
a.start()
time.sleep(5)
print("TIMES UP")
parent.send(a.pid)