Julia: Generate code at runtime and store it for future evaluation/execution

Viewed 80

In Julia, how one can generate arbitrary code to serialize it into a file for future execution under completely different program instance? Is it even possible? Is there a need to store any current context for future execution?

2 Answers

Here is a more detailed answer

  1. Put your code in a Julia package. This is something you should do with any production code - it is so much more convenient. Let say it is MyPackage

  2. Create a script that runs all methods in the package with types that you plan to use so the compiler knows which version of those methods to compile. Let us call it precompile_MyPackage.jl. You could use commands/scenarios that you use regularly plus perhaps test sets from MyPackage.

  3. Compile the package using PackageCompiler

using PackageCompiler
create_sysimage(:MyPackage, sysimage_path="sys_MyPackage.so", precompile_execution_file="precompile_MyPackage.jl")
  1. Once done you will be starting Julia with the following comand:
julia --sysimage sys_MyPackage.so

Finally, please have a look at the excellent tutorial video https://live.juliacon.org/talk/Z8TE39

Related