Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia invoke script on existing REPL from command line

I want to run a Julia script from window command line, but it seems everytime I run > Julia code.jl, a new instance of Julia is created and the initiation time (package loading, compiling?) is quite long.

Is there a way for me to skip this initiation time by running the script on the current REPL/Julia instance? (which usually saves me 50% of running time).

I am using Julia 1.0.

Thank you,

like image 932
Huy Tran Avatar asked Oct 17 '25 21:10

Huy Tran


2 Answers

You can use include:

julia> include("code.jl")
like image 67
fredrikekre Avatar answered Oct 20 '25 18:10

fredrikekre


There are several possible solutions. All of them involve different ways of sending commands to a running Julia session. The first few that come to my mind are:

  • use sockets as explained in https://docs.julialang.org/en/v1/manual/networking-and-streams/#A-simple-TCP-example-1
  • set up a HTTP server e.g. using https://github.com/JuliaWeb/HTTP.jl
  • use named pipes, as explained in Named pipe does not wait until completion in bash
  • communicate e.g. through the file system (e.g. make Julia scan some folder for .jl files and if it finds them there they get executed and moved to another folder or deleted) - this is probably simplest to implement correctly

In all the solutions you can send the command to Julia by executing some shell command.

No matter which approach you prefer the key challenge is sanitizing the code to handle errors properly (i.e. a situation when you sent some command to the Julia session and it crashes or when you send requests faster than Julia is able to handle them). This is especially important if you want the Julia server to be detached from the terminal.

As a side note: when using the Distributed module from stdlib in Julia for multiprocessing you actually do a very similar thing (but the communication is Julia to Julia) so you can also have a look how this module is implemented to get the feeling how it can be done.

like image 43
Bogumił Kamiński Avatar answered Oct 20 '25 16:10

Bogumił Kamiński



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!