2017-03-28 114 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

相關問題