Hi,
I am trying to open a process for both input and output, i.e. I want to be able to send it stuff and read back a result. So I tried the following code, which is supposed to start a Python interpreter, send it the expression “1+1”, and get the result back:
go :-
process_create(path(python), [],
[stdin(pipe(In)), stdout(pipe(Out)), process(Pid)]),
format("pid: ~w~n", [Pid]),
writeln(In, "1+1"),
flush_output(In), % apparently necessary!
read_line_to_string(Out, Result),
writeln(Result).
This shows me the PID but hangs when I try to get the result. If I replace python
with bash
, and “1+1” with “ls”, it works fine. I wonder if it has anything to do with the fact that Python prints some lines when it starts, and bash does not? In any case, what am I doing wrong? Any tips welcome.
(By the way, I am just using Python as an example here; I am interested in a general solution rather than in talking to Python specifically…)
Oh, and a somewhat-related question: Is it possible in SWI-Prolog to have a stream that e.g. writes to both stdout and a file? (This would have been useful here for debugging purposes, etc.)
Thanks,
–Hans Nowak