2012-04-08 31 views
0

這是繼續我以前的帖子。球拍程序和python程序之間的通信 - 2

communcation between racket program and python program

,如果我嘗試保持這個循環中被寫入和讀取持續,它不工作了。

我的球拍代碼:

#lang racket 

(define-values (sp i o e) 
    (subprocess #f #f #f "C:/Python26/python.exe" "C:/Python26/hello.py")) 

(define counter 40) 

(let loop() 
    (display "play\n" o)  
    (flush-output o)  
    (display (read-line i)) 
    (when (> counter 0) (loop))) 

我的Python代碼:hello.py

while 1: 
    input_var = raw_input() 
    print "you entered\n" 
+1

您可能希望考慮縮進代碼以使其更具可讀性(對於Python,可編譯)。 – 2012-04-08 00:10:51

+0

嘗試'sys.stdin.read()'。 – Blender 2012-04-08 00:12:52

+0

我用sys.stdin.readline()並沒有幫助。我的程序仍然掛起。 – chom 2012-04-08 00:24:15

回答

2

對事物的Python端的代碼可能無法沖洗:很可能是你再次遇到緩衝問題。嘗試使用-u標誌運行python,以在Python的一端強制執行無緩衝的輸出流。

How to flush output of Python print?Disable output buffering的回答也可能是相關的。

+0

這對我有幫助。 import sys print'這會立即輸出。' sys.stdout.flush() – chom 2012-04-08 22:54:14