作者thefattiger (LT)
看板Python
标题[问题] subprocess与java通讯问题
时间Tue Aug 21 11:11:52 2018
小弟有个Python程式如下
import subprocess
if __name__ == '__main__':
p = subprocess.Popen(["java","withPy"], stdin=subprocess.PIPE,stdout=subprocess.PIPE)
p.stdin.write(b"abc")
line=p.stdout.read()
print(line)
对应的Java程式:
import java.io.*;
public class withPy {
public static void main(String[] args) {
try {
BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
String s = bufferRead.readLine();
System.out.println(s);
}
catch(IOException e){
System.out.println(e);
}
}
}
其实就只是将abc传给Java再传回来
但执行後老是卡在p.stdout.read()那一行
已测试过若只有单方面的通讯(Python=>Java, Java=>Python)都是可行
但若要Python=>Java=>Python就一直试不成功
请问有前辈可以指点小弟该怎麽做吗?
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 125.227.45.150
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1534821114.A.75F.html
1F:→ dododavid006: 你 python 那边的 write 加个换行看看 因为你 java 08/21 14:18
2F:→ dododavid006: 这边用的是 readLine 08/21 14:18
3F:→ dododavid006: 然後 python 的 write 後加个 p.stdin.flush() 08/21 14:26
4F:→ renshin: 是执行後一直处於等待,还是有error code? 08/21 17:00
5F:→ renshin: 请问怎麽知道是卡在p.stdout.read()这行呢? 08/21 17:00
6F:→ thefattiger: 感谢一楼大大,加上换行字元跟flush就可以了 08/21 17:50
7F:→ thefattiger: 是用print知道卡在这边的 08/21 17:50
8F:→ thefattiger: 其实我後来改用socket处理通讯问题,现在可以改回来了 08/21 17:53