bashshelljuliaprecompileload-time

Julia from bash script


Can I create one instance of Julia and use it to run multiple Julia scripts from bash script?

#!/bin/bash
J=getjuliainstance()
J.run(temp.jl)
J.run(j1.jl)
J.run(j2.jl)
J.run(j3.jl)
J.exit()

I could run all of them from inside a master Julia script but that is not the intent.

This is to circumvent Julia's load time for the first script so that runtime of subsequent scripts can be timed consistently.

Any way to spawn a single process and reuse it to launch scripts? From shell script only please!

One of the solutions (allows for a tail -f):

julia <pipe 2>&1 | tee submission.log > /dev/null &

Solution

  • You can try named pipes:

    $ mkfifo pipe # create named pipe
    
    $ sleep 10000 > pipe & # keep pipe alive
    [1] 11521
    
    $ julia -i <pipe & # make Julia read from pipe
    [2] 11546
    
    $ echo "1+2" >pipe
    
    $ 3
    
    
    $ echo "rand(10)" >pipe
    
    $ 10-element Array{Float64,1}:
     0.938396
     0.690747
     0.615235
     0.298277
     0.780966
     0.775423
     0.197329
     0.136582
     0.302169
     0.607562
    
    
    $
    

    You can send any commands to Julia using echo. If you use stdout for Julia output then you have to press enter when Julia writes something there to return to prompt. Stop Julia by writing echo "exit()" >pipe. If you want to execute a file this way use include function.

    EDIT: it seems that you even do not have to use -i if you run Julia this way.

    EDIT2: I did not notice that actually you want to use only one bash script (not an interactive mode). In such a case it should be even simpler to use named pipes.