# Communicate with external process through pipe

**URL:** <https://discourse.julialang.org/t/communicate-with-external-process-through-pipe/59759>\
**Category:** General Usage\
**Created:** [April 21, 2021, 11:14pm UTC](https://discourse.julialang.org/t/communicate-with-external-process-through-pipe/59759 "2021-04-21T23:14:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lambdakappatheta](https://avatars.discourse-cdn.com/v4/letter/l/bbce88/32.png) [@lambdakappatheta](https://discourse.julialang.org/u/lambdakappatheta)\
**Post date:** [April 21, 2021, 11:14pm UTC](https://discourse.julialang.org/t/communicate-with-external-process-through-pipe/59759/1 "2021-04-21T23:14:45Z")

</div>

I am trying to create a pipe (a pair of file descriptors), find the file descriptor number of the writing end of the pipe, pass it to an external command and read from the reading end of the pipe from my Julia script. In short, I need to create a pipe, run an external command and make sure that it inherits the appropriate file descriptor from my script. From [Running External Programs](https://docs.julialang.org/en/v1/manual/running-external-programs/), I’ve learned about [Cmd](https://docs.julialang.org/en/v1/base/base/#Base.Cmd) and [run](https://docs.julialang.org/en/v1/base/base/#Base.run) and from [this](https://discourse.julialang.org/t/collecting-all-output-from-shell-commands/15592/2) answer about the Pipe type which I am unable to locate using the search bar at [docs.julialang.org](http://docs.julialang.org). I tried running

```julia
p = Pipe()
println(p)

```

from a script that came back with a stack trace which I was able to use to locate the definitions of the [PipeEndpoint](https://github.com/JuliaLang/julia/blob/7fe05db29340aa42c280290ffc922d8dc0e20c72/base/stream.jl#L169) and [Pipe](https://github.com/JuliaLang/julia/blob/7fe05db29340aa42c280290ffc922d8dc0e20c72/base/stream.jl#L684) types. From here, I could start reading the source but I figured I shoud double-check first whether I’m on the right track at all.

EDIT:

This is what I’m trying to do in Python:

```python
import os
import subprocess

r, w = os.pipe()

command = ["perf", "stat"] + ["--log-fd",
                              f"{w}"] + ["dd", "if=/dev/zero", "of=/dev/null", "count=1000000"]

popen = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE, pass_fds=[w], text=True)

stdout, stderr = popen.communicate()
popen.wait()

os.close(w)
file_object = open(r)
stats = file_object.read()
file_object.close()

print(f"stdout:\n{stdout}")
print(f"stderr:\n{stderr}")
print(f"stats:\n{stats}")

```

In Julia, I tried using a simple file instead of a pipe but it seems that the file descriptor is not kept open between the parent and child:

```julia
pstdout = Pipe()
pstderr = Pipe()

io = open("stats", "w+")
filedescriptor = fd(io)

proc = run(pipeline(`perf stat --log-fd $filedescriptor dd if=/dev/zero of=/dev/null count=1000000`, stdout = pstdout, stderr = pstderr), wait=false)

close(pstdout.in)
close(pstderr.in)

stdout = String(read(pstdout))
stderr = String(read(pstderr))

println("stdout:")
println(stdout)

println("stderr:")
println(stderr)

```

---

<div class="post-metadata">

**Author:** ![mbaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbaz/32/17295_2.png) [@mbaz](https://discourse.julialang.org/u/mbaz)\
**Post date:** [April 21, 2021, 11:32pm UTC](https://discourse.julialang.org/t/communicate-with-external-process-through-pipe/59759/2 "2021-04-21T23:32:20Z")

</div>

I’m not sure if this is what you need, but this is what I do to communicate with an external process using pipes:

```julia
pstdin = Pipe()
pstdout = Pipe()
pstderr = Pipe()
proc = run(pipeline(`externalprogram`,
                    stdin = pstdin, stdout = pstdout, stderr = pstderr),
                    wait = false)
process_running(proc) || error("There was a problem.")
close(pstdout.in)
close(pstderr.in)
close(pstdin.out)

```

At this point, you can communicate with the external program by writing to `pstdin`, and reading from `pstdout` and `pstderr`.

---

<div class="post-metadata">

**Author:** ![lambdakappatheta](https://avatars.discourse-cdn.com/v4/letter/l/bbce88/32.png) [@lambdakappatheta](https://discourse.julialang.org/u/lambdakappatheta)\
**Post date:** [April 22, 2021, 10:54am UTC](https://discourse.julialang.org/t/communicate-with-external-process-through-pipe/59759/3 "2021-04-22T10:54:28Z")

</div>

Thank you for your reply! Communicating through stdin, stdout and stderr is only half of the story… I edited my question to make it clearer what I am after.
