我正在尝试在父进程和子进程之间创建管道:
index()
此代码可以启动子进程,但firstsentence = input('Enter a sentence: ')
firstsentence = firstsentence.lower()
words = firstsentence.split(' ')
s = ""
for word in words:
s += str(words.index(word)+1) + " "
print(s)
阻止运行而没有任何结果。怎么了?
编辑子进程运行以下循环echo-program(" echo.py"):
def launch_process():
parentStdin, childStdout = os.pipe()
childStdin, parentStdout = os.pipe()
pid = os.fork()
if pid:
os.close(childStdout)
os.close(childStdin)
self.set_configuration()
else:
os.close(parentStdout)
os.close(parentStdin)
os.dup2(childStdin, 0)
os.dup2(childStdout, 1)
os.execvp("echo.py", "")
raise RuntimeError
#after launch_process() call...
pipein = os.fdopen(parentStdin) #I saved both parentStdin and parentStdout variable as global
while True:
time.sleep(3)
os.write(parentStdout, ("command").encode())
line = pipein.readline() #the program stops here
print(line)