我尝试编写一个python脚本,该脚本从Java文件接收数据并将数据发送给它,但是看来该脚本仅接收数据而不发送数据。 这是python代码:
def java_python_comu():
compile_java = subprocess.Popen(["javac", "MultiThread.java"])
compile_java.communicate()
java_io = subprocess.Popen(["java", "MultiThread"], stdout=subprocess.PIPE, stdin=subprocess.PIPE)
read_java_output = threading.Thread(target=read, args=(java_io,))
write_java_input = threading.Thread(target=write_func, args=(java_io, ))
write_java_input.start()
read_java_output.start()
def write(pipe, text):
with MUTEX_LOCK:
pipe.stdin.write(str2bytes(text + "\n"))
def write_func(pipe):
write(pipe, "_RIGHT_")
write(pipe, "_LEFT_")
write(pipe, "_UP_")
write(pipe, "_DOWN_")
write(pipe, "_STOP_")
write(pipe, EXIT_VALUE)
def read(pipe):
with MUTEX_LOCK:
line = pipe.stdout.readline()
line = str(line.decode("utf-8"))
while not line.count(EXIT_VALUE):
if not (line == "" or line.count(EXIT_VALUE) or enter(line)):
print("JAVA: " + line, end="")
with MUTEX_LOCK:
line = pipe.stdout.readline()
line = str(line.decode("utf-8"))
print("END")
这是Java代码
import java.io.*;
class JavaClass implements Runnable{
String name;
Thread t;
JavaClass (String threadname){
name = threadname;
t = new Thread(this, name);
t.start();
}
public void writeToFile(String str){
File file = new File("1003.txt");
FileWriter fr = null;
try {
fr = new FileWriter(file, true);
fr.write(str + "\n");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void read(){
BufferedReader bufferRead = null;
String s = "";
try{
bufferRead = new BufferedReader(new InputStreamReader(System.in));
}catch(Exception e){
writeToFile(e.toString());
}
try{
s = bufferRead.readLine();
} catch(Exception e) {
writeToFile(e.toString());
}
writeToFile("s: " + s);
String error1 = "";
String error2 = "";
while(s == null || !s.equals("_EXIT_")){
try{
if(s != null){
if(s.equals("_RIGHT_")){
writeToFile("ROBOT: moves backwards");
}else if(s.equals("_LEFT_")){
writeToFile("ROBOT: moves backwards");
}else if(s.equals("_UP_")){
writeToFile("ROBOT: moves backwards");
}else if(s.equals("_DOWN_")){
writeToFile("ROBOT: moves backwards");
}
}
}catch(Exception e){
String tmpError = e.toString();
if(!tmpError.equals(error1)){
error1 = tmpError;
writeToFile("1" + error1);
}
}
try{
s = bufferRead.readLine();
}catch(Exception e){
String tmpError = e.toString();
if(!tmpError.equals(error2)){
error2 = tmpError;
writeToFile("2" + error2);
}
}
}
}
public void output(){
System.out.println("This is the java code");
System.out.println("Read by the python code");
System.out.println("And compiled by it");
}
public void run() {
try{
switch (t.getName()){
case "input":
read();
break;
case "output":
output();
break;
}
}catch (Exception e) {
System.out.println(name + "Interrupted");
}
}
}
public class MultiThread {
public static void main(String args[]) {
new JavaClass("input");
new JavaClass("output");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
System.out.println("Main thread exiting.");
System.out.println("__EXIT__");
}
}
该代码没有给出任何错误,并且每次我检查s时,它都表示它为空。
当我将其分离到不同的文件(一个用于发送信息的脚本和一个用于接收信息的脚本)时,它可以完美地工作。