2

I'm trying to call Julia scripts with Python to get some better speeds on functions I need to call a huge number of times (for some Monte Carlo type analyses). I've sucessfully installed and run Julia scripts in python via:

from julia import Main as JuliaMain  # julia().<something> --> is deprecated, use Main instead
# julia.install()  # use when running for the first time
script_julia = JuliaMain.include('juliascript.jl')

in addition to ensuring that PyCall is configured correctly in the Julia runtime.

Please correct me if I am wrong, but with my current understanding, this will compile any functions inside juliascript.jl in the sense that Julia will figure out what the return type is and go through whatever motions it does when you call and/or define a function for the first time.

Consider that the defined function in juliascript.jl is something like:

function f(x,y)
    z = x + y
    return z
end

If I call this function with x = JuliaMain.eval("f(1, 3)"), will the speed of this function reflect a Julia function called more than once, or is it effectively being "recompiled" each time? Furthermore, how can I call these defined functions without having to convert my request to a string to do so? I know from the docs that you can set and get variables directly, but this seems quite clumsy, and I was hoping there was a more elegant way, such as something in the format of JuliaMain.f(x, y) or JuliaMain.runfunction.f(x, y), etc.

Will
  • 170
  • 10
  • 1
    I believe this is a duplicate of this question https://stackoverflow.com/questions/64241264/i-have-a-high-performant-function-written-in-julia-how-can-i-use-it-from-python/64241265#64241265 however I might be biased here. If it is missing some information that you need please let me know – Przemyslaw Szufel Feb 09 '21 at 18:45
  • Good reference! I didn't see this. Is moving it into a package a strict requirement, or is there a way to go from where I am currently at (using .include from a script within the same folder) to running the function from something like `myjuliascript.f([1,2],5)`? – Will Feb 09 '21 at 20:41
  • 1
    Packages get precompiled (so they are faster in subsequent runs) and can be loaded as a package in Python. Moreover they come with their own virtual environment via `Project.toml`. Why it is not a strict requirement you would need a very good reason no to do so and generally not using a package would make your life (including production deployments) much more painful. Last but not least a Julia package can be statically compiled into Julia's system image. – Przemyslaw Szufel Feb 09 '21 at 20:49
  • Perfect, this answers both questions, actually, as I was about to ask if the method I was using was precompiling the function, but it appears that it is not. If you put this into a response, I will mark it as the answer. – Will Feb 09 '21 at 20:52
  • since many people rely on this knowledge I rather closed this question and edited my previous post with comments to your answer. I prefer to keep all details in one place so people can easier find all info they need :-) – Przemyslaw Szufel Feb 09 '21 at 21:04

0 Answers0