I have a program from which I want to read, lets say its python. So I have these 2 functions:
(defun start-python ()
(let ((process
(sb-ext:run-program "/usr/bin/python" nil
:output :stream
:input :stream
:wait nil
:search t
:error *standard-output*)))
process))
(defun test-process-stream ()
(let ((process (start-python)))
(format (sb-ext:process-input process) "print 'hello world!'~%")
(finish-output (sb-ext:process-input process))
;;I do not want to call "close" because I will be still reading from the input
(close (sb-ext:process-input process))
(print (read-line (sb-ext:process-output process)))
(when (listen (sb-ext:process-output process))
(print (read-line (sb-ext:process-output process))))
(close (sb-ext:process-output process))
(sb-ext:process-close process)
))
I want to be able to incrementally read from the output of the python process while at the same time provide input to it. I have tried with several methods even ones mentioned here: SBCL: Gather output of run-program process while running
But I have not been able to do this in SBCL.
In the example code I call close
because that is the only way I get any output at all. Otherwise it just hangs.
I would much appreciate any pointers because I am stuck at this. I even tried with (listen ...)
and (finish-output ...)
and still it hangs on (read-line ...)
. The only difference with (listen ...)
is that it returns false and nothing is printed. I even tried (sleep 2)
before trying to read. Still nothing.
EDIT: Ultimately my goal is to run swipl
which is SWI-Prolog. I used python here as an example. I want to achive interoperability between lisp and prolog such that I could issue queries to prolog and read in back responses. Currently I couldn't find any projects or libraries that would be suited for my needs, so that is why I am attempting this.