【发布时间】:2017-08-21 17:44:18
【问题描述】:
我通过父代码将字符串Router_Status[Router]='ON'发送到新进程
proc[Router] = subprocess.Popen([sys.executable, os.getcwd() + '/'
+ Router + '.py', Router,
json.dumps(graph),
json.dumps(As_numbers_dict)],
shell=False, stderr=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
proc[Router].stdin.write(bytes(Router_Status[Router],
encoding='utf-8') + b'\n')
子进程是
Router_Status[Router]=sys.stdin.readline().strip()
path = os.path.expanduser('~' + '/BGP_Routers/' + Router)
with open(path + '/Router_Status.txt', 'w') as f:
f.write(Router_Status[Router])
但它不起作用! 然后我将第二个字符串 Router_Status[Router]='OFF' 通过 proc[Router].stdin.write(bytes(Router_Status[Router], encoding='utf-8')
proc[Router].stdin.flush()
它仍然没有做任何事情!
【问题讨论】:
-
不起作用怎么办?孩子卡住了?您是在父进程中使用
proc[Router].wait()还是立即退出? -
我的子进程有一些无限循环子进程,直到子进程收到新的字符串='OFF'。我想发送两个标准输入。在我的子进程中编写和阅读它们。它也不会生成文件,也不会在文件中写入“ON”或“OFF”。文件路径正确。
-
你试过
proc[Router].stdin.close()吗?只是看看它在做什么 -
是在 proc[Router].stdin.write(bytes(Router_Status[Router],encoding='utf-8') + b'\n') 之后我写了 proc[Router].stdin。 close() 但第二次当我想发送'OFF'时出错'ValueError: write to closed file'
-
stderr=True看起来也很可疑。你能设置成stderr=subprocess.STDOUT吗?
标签: python python-3.x subprocess