0

This is in continuation with my previous post.

communcation between racket program and python program

if i try to keep this in loop that is writing and reading continuously, It is not working again.

My Racket code :

#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)))

My python code : hello.py

while 1: 
    input_var = raw_input() 
    print "you entered\n"
Community
  • 1
  • 1
chom
  • 265
  • 1
  • 5
  • 16
  • 1
    You may wish to consider indenting your code to make it more readable (and in the case of Python, compilable). – Greg Hewgill Apr 08 '12 at 00:10
  • I used sys.stdin.readline() and it didn't help. Still my program hangs. – chom Apr 08 '12 at 00:24
  • Is that loop intended to ever terminate? You don't actually decrement the counter at any point. Also, the more natural idiom in most Racket programs is to use the argument to track the loop index rather than a mutable variable. – Asumu Takikawa Apr 08 '12 at 01:48
  • Yeah, i am not decrementing the counter. It becomes an infinite loop. My python code has also got an infinite loop. Anyhow it should work with infinite loops displaying the output from python program. – chom Apr 08 '12 at 02:06

1 Answers1

2

The code on the Python side of things may not be flushing: it's likely that you're running into buffering issues again. Try running python with the -u flag to force unbuffered output streams on the Python side of things.

The answers to How to flush output of Python print? and Disable output buffering may also be relevant.

Community
  • 1
  • 1
dyoo
  • 11,795
  • 1
  • 34
  • 44
  • This helped me. import sys print 'This will be output immediately.' sys.stdout.flush() – chom Apr 08 '12 at 22:54