2012-04-08 32 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