# How to interact with a Julia process inside a Julia process

**URL:** https://discourse.julialang.org/t/how-to-interact-with-a-julia-process-inside-a-julia-process/68460
**Category:** General Usage
**Created:** [September 20, 2021, 12:40pm UTC](https://discourse.julialang.org/t/how-to-interact-with-a-julia-process-inside-a-julia-process/68460 "2021-09-20T12:40:01Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [September 20, 2021, 12:40pm UTC](https://discourse.julialang.org/t/how-to-interact-with-a-julia-process-inside-a-julia-process/68460/1 "2021-09-20T12:40:01Z")

</div>

I want to measure compilation and TTFP latencies. So my idea was to run different scripts in fresh Julia processes which I start from a main Julia process so that each time Julia has to reliably compile everything anew.

There are two problems:

- How can I keep a Julia process open and run separate chunks of code in it synchronously? For example, first I would run `@timed using Package`, then do something with the result in the main Julia process, then execute the next block that makes further use of `Package`.

I tried something like

```julia
open(`julia`, "w", some_iobuffer) do process
    println(process, some_commands)
    do_something_with_some_iobuffer()
    println(process, some_more_commands)
    do_something_else_with_some_iobuffer()
end

```

The problem is that `println(process, ...` is asynchronous, so the iobuffer doesn’t contain the expected written out data afterwards. That leads me to the second question:

- How can I send data from the child process to the main process? In practice that would just be a couple of NamedTuples without any special other types embedded inside of them. Currently, because the iobuffer method doesn’t work, I’m writing to a csv file, but that seems clunky.

---

<div class="post-metadata">

### Author: ![JonasIsensee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonasisensee/32/4704_2.png) [@JonasIsensee](https://discourse.julialang.org/u/JonasIsensee)
#### Post date: [September 20, 2021, 2:01pm UTC](https://discourse.julialang.org/t/how-to-interact-with-a-julia-process-inside-a-julia-process/68460/2 "2021-09-20T14:01:15Z")

</div>

Would it have to be a _separate_ julia process? (more so than explicitly adding a worker process)

```julia
julia> using Distributed

julia> @time using DataFrames
  1.043179 seconds (1.73 M allocations: 118.945 MiB, 2.35% gc time, 0.50% compilation time)

julia> w = addprocs(1)
1-element Vector{Int64}:
 2

julia> @time @everywhere 2 begin; @time using DataFrames; end
      From worker 2: 0.933681 seconds (1.52 M allocations: 106.554 MiB, 6.33% gc time, 0.51% compilation time)
  1.405225 seconds (91 allocations: 4.406 KiB)

```

returning results would be as easy as

```julia
timing_result = remotecall_fetch(2) do # arg is the worker id
    return remote_global_variables
end

```

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [September 20, 2021, 2:42pm UTC](https://discourse.julialang.org/t/how-to-interact-with-a-julia-process-inside-a-julia-process/68460/3 "2021-09-20T14:42:48Z")

</div>

Ah that’s interesting, I didn’t think of using `Distributed`. Well my goals are just that the process has to compile the packages used completely from scratch, and that it uses a different environment than the calling code. I’ll try it out!
