# Using a bi-directional pipe to read and write from 'cmd'

**URL:** <https://discourse.julialang.org/t/using-a-bi-directional-pipe-to-read-and-write-from-cmd/30313>\
**Category:** General Usage\
**Created:** [October 25, 2019, 6:23pm UTC](https://discourse.julialang.org/t/using-a-bi-directional-pipe-to-read-and-write-from-cmd/30313 "2019-10-25T18:23:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ko56](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ko56/32/2684_2.png) [@ko56](https://discourse.julialang.org/u/ko56)\
**Post date:** [October 25, 2019, 6:23pm UTC](https://discourse.julialang.org/t/using-a-bi-directional-pipe-to-read-and-write-from-cmd/30313/1 "2019-10-25T18:23:42Z")

</div>

Hi,

I’m using a bi-directional pipe for Julia 1.1.1 to communicate back and forth with an external process:

```julia
cvc4 = open(`cvc4 --incremental`, read=true, write=true)

```

The 2-way communication seems to work ok, except when I try to read a multi-line response from the process, and I don’t know a priori how many lines it consists of.

Let’s say the response is 3 lines. This works:

```julia
println(readline(cvc4.out))
println(readline(cvc4.out))
println(readline(cvc4.out))

```

But the obvious way of reading an unknown number of lines hangs:

```julia
println(readlines(cvc4.out))

```

I have tried various things, like readline() while !eof(cvc4.out), but nothing works.  
Any help would be appreciated.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [October 25, 2019, 6:30pm UTC](https://discourse.julialang.org/t/using-a-bi-directional-pipe-to-read-and-write-from-cmd/30313/2 "2019-10-25T18:30:44Z")

</div>

You could just call `read(cvc4, String)` and then split the lines yourself, via `split(read(cvc4, String), '\n')`, or perhaps you could also do:

```julia
while isreadable(cvc4)
  readline(cvc4)
end

```

---

<div class="post-metadata">

**Author:** ![ko56](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ko56/32/2684_2.png) [@ko56](https://discourse.julialang.org/u/ko56)\
**Post date:** [October 25, 2019, 7:41pm UTC](https://discourse.julialang.org/t/using-a-bi-directional-pipe-to-read-and-write-from-cmd/30313/3 "2019-10-25T19:41:19Z")

</div>

Thanks. The first solution doesn’t work (hangs). The second one produces all the lines of the answer, and then hangs.  
(Whether I run it from the REPL or from the command line).

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [October 26, 2019, 3:20am UTC](https://discourse.julialang.org/t/using-a-bi-directional-pipe-to-read-and-write-from-cmd/30313/4 "2019-10-26T03:20:13Z")

</div>

I think you need at least one `@async` (see e.g., [Collecting all output from shell commands - #7 by tkf](https://discourse.julialang.org/t/collecting-all-output-from-shell-commands/15592/7)), unless there is some kind of a marker from the external process that indicates the end of multi-line response. But, if you need to deal with multiple requests and responses, I don’t think there is a way to reliably relate a chunk of output lines to a response. So, using `@async` may not solve the actual problem.

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [October 27, 2019, 1:35pm UTC](https://discourse.julialang.org/t/using-a-bi-directional-pipe-to-read-and-write-from-cmd/30313/5 "2019-10-27T13:35:05Z")

</div>

You could use `readuntil` with an EOT character

> **[End-of-Transmission character](https://en.wikipedia.org/wiki/End-of-transmission_character)**
>
> In telecommunication, an end-of-Transmission character (EOT) is a transmission control character. Its intended use is to indicate the conclusion of a transmission that may have included one or more texts and any associated message headings.
> An EOT is often used to initiate other functions, such as releasing circuits, disconnecting terminals, or placing receive terminals in a standby condition. Its most common use today is to cause a Unix terminal driver to signal end of file and thus exit progra...

---

<div class="post-metadata">

**Author:** ![ko56](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ko56/32/2684_2.png) [@ko56](https://discourse.julialang.org/u/ko56)\
**Post date:** [October 28, 2019, 9:57pm UTC](https://discourse.julialang.org/t/using-a-bi-directional-pipe-to-read-and-write-from-cmd/30313/6 "2019-10-28T21:57:15Z")

</div>

I don’t know how @async works, and I don’t have control over the external process with which I’m communicating, so the EOT idea doesn’t work.

Anyhow, the solution turns out to be simple: Julia’s readavailable().  
(Plus some processing of the result.)
