GO:是否有某種方式與正在等待stdin輸入的子進程(shell腳本/ python腳本)進行通信?Go子進程通信
例如python腳本(子)
import sys
while True:
sys.stdout.write('%s\n'%eval(sys.stdin.readline()))
在走程序,我想創造這個python腳本的子進程,並提供它的標準輸入,在有需要時,反覆輸入,並採取它的輸出。在Go程序的標準輸出或從文件中讀取/寫入也是可行的。
這大致是什麼,我想,但沒有任何反應 -
c := exec.Command("python", "-u add.py")
si,_ := c.StdinPipe()
so,_ := c.StdoutPipe()
c.Start()
si.Write([]byte("2+2\n")
無關:你可以用'輸入'取代的eval(sys.stdin.readline())'()'關於Python 2.或者替換'進口整個循環碼; shell = code.InteractiveConsole(); shell.interact()'或者直接啓動'python -i'進程。 – jfs
這就是爲什麼你不應該忽略錯誤,試試'exec.Command(「python」,「-u」,「add.py」)或者'exec.Command(「python」,「-u」,「 -i「)或exec.Command(」python「,」-i「)'(如果它不使輸出完全緩衝) – jfs
沒有錯誤。仍然無法正常工作 –