communcation between racket program and python program

二次信任 提交于 2020-01-14 10:39:05

问题


I want communication between racket program and python program.

My Racket code:

#lang racket

(define-values (sp o i e) (subprocess #f #f #f "hello.exe" ))

(display "server" i)

(flush-output i)

(display (read o))

My python code:

input_var = raw_input("Enter something: ")

print ("you entered " + input_var)

If i am just printing in my python program it works fine. If i am reading input from racket program it hangs. I want to read messages from racket.


回答1:


It looks like it's hanging because you failed to issue a newline (\n) to the output port. Here's how I ran your code:

#lang racket

(define-values (sp i o e) (subprocess #f #f #f
                                      "/usr/bin/python"
                                      "/tmp/foo.py"))

(display "server\n" o)

(flush-output o)

(display (read-line i))

... with the code you supplied in "/tmp/foo.py", and I saw the output:

Enter something: you entered server

... which is what I expected.

The only interesting difference here is that I appended a newline character to the output.

Note also that I swapped the names of your "o" and "i", because I didn't like the fact that "o" was an input port.



来源:https://stackoverflow.com/questions/9989009/communcation-between-racket-program-and-python-program

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!