# Using process\_create for both input and output

**URL:** <https://swi-prolog.discourse.group/t/using-process-create-for-both-input-and-output/664>\
**Category:** Help!\
**Created:** [May 18, 2019, 4:55pm UTC](https://swi-prolog.discourse.group/t/using-process-create-for-both-input-and-output/664 "2019-05-18T16:55:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zephyrfalcon](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/zephyrfalcon/32/224_2.png) [@zephyrfalcon](https://swi-prolog.discourse.group/u/zephyrfalcon)\
**Post date:** [May 18, 2019, 4:55pm UTC](https://swi-prolog.discourse.group/t/using-process-create-for-both-input-and-output/664/1 "2019-05-18T16:55:34Z")

</div>

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:

```prolog
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

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [May 18, 2019, 5:23pm UTC](https://swi-prolog.discourse.group/t/using-process-create-for-both-input-and-output/664/2 "2019-05-18T17:23:19Z")

</div>

Welcome,

While others such as @Jan will most likely give a more exact and helpful answer, one thing that you did not note and that might be of help is to look at the actual code for SWI-Prolog in the GitHub [repository](https://github.com/SWI-Prolog).

Be aware that some of the examples may be old as I recently [learned](https://swi-prolog.discourse.group/t/using-call-cleanup-2-trying-to-understand-when-to-use-it-or-others-predicates-of-the-same-intention/657) but ask if you are not certain.

Hope this helps.

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [May 18, 2019, 5:53pm UTC](https://swi-prolog.discourse.group/t/using-process-create-for-both-input-and-output/664/3 "2019-05-18T17:53:23Z")

</div>

Here’s a hint:

```prolog
$ python </dev/null
$ swipl
process_create(path(python), [], [stdin(null), stdout(pipe(Out))]), read_line_to_string(Out, Result).

```

Pipes in Unix are non-trivial if you have both stdin and stdout. It’s been ages since I’ve programmed with them, but my recollection is that Steven’s “Advanced Unix” book is a good source for avoiding the gotchas. (Stack exchange, of course, is another.)

Things to watch out for: buffering, the size of the pipe, blocking I/O.
