2017-03-28 73 views
1

发送多个字符串我发送字符串Router_Status [路由器] =“ON”由父代码来新工艺如何通过标准输入Python中

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 [路由器] = 'OFF' 的过程由 PROC [路由器] .stdin.write(字节(Router_Status [路由器],编码= 'UTF-8')

proc[Router].stdin.flush() 

它仍然没有做任何事情

+0

不工作怎么样?小孩呆住了吗?你在你的父进程中使用'proc [Router] .wait()'还是一次退出? –

+0

我的子进程有一些无限循环子进程,直到子进程接收到新的字符串='OFF'。我想发送两个stdin。在我的子进程中编写并阅读它们。它也不会生成文件,也不会在文件中写入'ON'或'OFF'。文件路径是正确的。 –

+0

你尝试过'proc [Router] .stdin.close()'?只是为了看看它在做什么 –

回答

0

好的,所以我不完全熟悉你传递给你的子进程的参数.Popen(),所以我无法检查这些是否正确。一个或两个关于子进程模块,并且有一些事情要考虑查看您的代码:

  • 使用shell=False不需要明确定义,因为它是标准设置(尽管如果您想明确说明某个原因,那当然是正确的)。
  • 参数stderr=False不正确。它必须是None或类似subprocess.PIPE

而且,由于使用的是Python3.x的文档指出:

Warning - Use communicate() rather than .stdin.write, .stdout.read or .stderr.read to avoid deadlocks due to any of the other OS pipe buffers filling up and blocking the child process.

所以你会用更好的proc[Router].communicate(input="your standard input, meaning your stdin")

报告还指出:

Popen.stdin - If the stdin argument was PIPE, this attribute is a writeable stream object as returned by open(). If the encoding or errors arguments were specified or the universal_newlines argument was True, the stream is a text stream, otherwise it is a byte stream. If the stdin argument was not PIPE, this attribute is None.

在你的情况下,意味着标准确实应该由字节流。

全部测试,我猜你应该这样做:

proc[Router] = subprocess.Popen([sys.executable, os.getcwd() + '/' 
           + Router + '.py', Router, 
           json.dumps(graph), 
           json.dumps(As_numbers_dict)], 
           stderr=subprocess.PIPE, 
           stdin=subprocess.PIPE, 
           stdout=subprocess.PIPE) 

proc[Router].stdin.communicate(str(Router_Status[Router]).encode()) 

我仍然不知道但是,你为什么要包括:

path = os.path.expanduser('~' + '/BGP_Routers/' + Router) 
with open(path + '/Router_Status.txt', 'w') as f: 
     f.write(Router_Status[Router]) 

由于它基本上是无关的subprocess.Popen ()语句。它所做的就是将用户输入写入文本文件,该文件只会捕获最新的输入btw。如果您希望将所有用户输入更改'w'保存为'a'(这将使文件处于追加模式而不是写入模式)。

+0

谢谢你的帮助。这是关于路由器。我必须将'ON'或'OFF'字符串发送到脚本以将其作为路由器的Os运行。例如Router_statues ='ON'。然后在[sys.executable,os.getcwd()+'/'+ Router +'.py'我只是调用脚本并将'ON'传递给它。我的问题是如何在我的脚本中读取'ON'字符串? –

+0

你想调用命令行的含义:'[sys.executable,os.getcwd()+'/'+ Router +'.py']',并简单地将''ON''作为参数传递?如果是的话,你为什么不尝试'[sys.executable,os.getcwd()+'/'+ Router +'.py','ON']'这将做到这一点......或者你是否意味着你想打开'〜/ Router.py'并阅读它的内容,看它是否被设置为''ON''或'OFF''? – Montmons

+0

它可能有助于添加一些[伪代码](http://stackoverflow.com/a/41517236/4041795)到你的问题来澄清你的意思..因为它仍然有点模糊,否则.. – Montmons

相关问题