最近,我一直在工作的管道和覆盆子皮。我正试图向我的函数发送一个信号来杀死它,但是"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)发布于 2022-08-11 11:44:40
您正在发送,并试图从管道的同一端检索该项。尝试这样做,在这里,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)https://stackoverflow.com/questions/73319634
复制相似问题