2017-12-29 36 views
0

파이썬 스크립트를 실행해야하는 자바 프로그램을 작성 중입니다.
스크립트는 스크립트의 진행 상태를 알기 위해 java가 읽을 출력을 출력합니다.
실행 중에 스크립트를 일시 중지 할 수있게하려면 잠시 동안 입력을 요청해야합니다. 자바가 입력 할 때만 스크립트가 계속 진행됩니다. 이건 내 파이썬 스크립트입니다
자바에서 파이썬 스크립트를 실행하고 입출력을 보내십시오.

private static void sevenTry(String[] strCommands) throws IOException { 
    Object oLock1 = new Object(); 
    Object oLock2 = new Object(); 

    ProcessBuilder pBuilder = new ProcessBuilder(strCommands); 
    pBuilder.redirectErrorStream(true); 
    Process proc = pBuilder.start(); 

    Thread tReader = new Thread() { 
     @Override 
     public void run() { 
      System.out.println("~~tReader starting~~"); 
      BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream())); 
      synchronized (oLock1) { 
       try { 
        String line = reader.readLine(); 
        while (line != null && !line.trim().equals("--EOF--")) { 
         System.out.println("Stdout: " + line); 
         if (line.trim().equals("--INPUT--")) { 
          synchronized (oLock2) { 
           oLock2.notify(); 
          } 
          oLock1.wait(); 
         } 
         line = reader.readLine(); 
        } 
       } catch (IOException e) { 
        System.out.println("tReader: " + e.getMessage()); 
       } catch (InterruptedException e) { 
        System.out.println("tReader: " + e.getMessage()); 
       } catch (Exception e) { 
        System.out.println("tReader: " + e.getMessage()); 
       } 
      } 
      System.out.println("~~tReader end~~"); 
      synchronized (oLock2) { 
       oLock2.notify(); 
      } 
     } 
    }; 
    Thread tWriter = new Thread() { 
     @Override 
     public void run() { 
      System.out.println("~~tWriter starting~~"); 
      BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())); 

      String line, input; 
      Scanner scan = new Scanner(System.in); 

      synchronized (oLock2) { 
       try { 
        oLock2.wait(); 
       } catch (InterruptedException e1) { 
        System.out.println("tWriter: " + e1.getMessage()); 
       } 
      } 
      while (tReader.isAlive()) { 
       synchronized (oLock1) { 
        System.out.println("Java: insert input"); 
        scan.hasNext(); 
        input = scan.nextLine(); 
        try { 
         writer.write(input + "\n"); 
         writer.flush(); 
        } catch (IOException e) { 
         System.out.println("tWriter: " + e.getMessage()); 
        } 
        oLock1.notify(); 
       } 
       try { 
        Thread.sleep(2000); 
       } catch (InterruptedException e1) { 
        System.out.println("tWriter: " + e1.getMessage()); 
       } 
      } 
      System.out.println("~~tWriter end~~"); 
     } 
    }; 

    tReader.start(); 
    tWriter.start(); 
    System.out.println("~~everything submitted~~"); 
    try { 
     tReader.join(); 
     tWriter.join(); 
     System.out.println("~~finish~~"); 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    } 
} 

:
여기 내 자바 방법입니다

# coding=utf-8 
import sys 

print '1' 
print '--INPUT--' 
inum = sys.stdin.readline() 
print '2' 
print '--EOF--' 

내 코드를 실행하는 시도

sevenTry("python", "C:\\Testing.py"); 

하지만 자바 측이 박히 내부 tReader 내부 :

String line = reader.readLine(); 

내가 (메신저 파이썬 2.7을 사용하여)
같은 문제를 가지고 여전히

inum = raw_input() 

를 사용하여 파이썬 파일

inum = sys.stdin.readline() 

에서 입력 라인을 가지고 있다면이 프로그램은 작동 하는가 여기에서 가장 혼란스러운 부분은 파이썬 대신 java 파일로 테스트하려고 시도했습니다.

이하고 입력 라인
당신이다시피

import java.util.Scanner; 

public class CheckCMD { 
    public static void main(String[] args) { 
     Scanner in = new Scanner(System.in); 
     String line; 
     System.out.println("1"); 
     System.out.println("--INPUT--"); 
     in.hasNext(); 
     line = in.nextLine(); 
     System.out.println("2"); 
     System.out.println("--EOF--"); 
    } 
} 

답변

0

와도 일이 파이썬과 관련된 문제입니다.

https://unix.stackexchange.com/questions/182537/write-python-stdout-to-file-immediately 한 바와 같이

공정 STDOUT은 단말 이외로 리디렉션 될 때

"는, 출력 일부 OS 특정 크기의 버퍼 (많은 경우에 아마도 4K 또는 8K)에 버퍼링된다. "

따라서 호출 할 때마다 sys.stdout.flush()을 호출해야합니다 (print).

또는 더 나은 옵션으로 -u 매개 변수를 사용하여 프로세스의 기본 동작을 변경하여 버퍼되지 않은 출력을 얻을 수 있습니다.