Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 867
Slaus Avatar asked Aug 12 '26 02:08

Slaus


2 Answers

I think you are looking for PackageCompiler.jl (https://github.com/JuliaLang/PackageCompiler.jl)

like image 72
jerlich Avatar answered Aug 15 '26 05:08

jerlich


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

like image 39
Przemyslaw Szufel Avatar answered Aug 15 '26 04:08

Przemyslaw Szufel



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!