Created
October 3, 2018 01:05
-
-
Save tkf/4ff242bb7039ce8c5b0897b4d2269106 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from ctypes import c_char_p, c_void_p | |
import ctypes | |
import os | |
import subprocess | |
def juliainfo(): | |
out = subprocess.check_output( | |
["julia", "-e", """ | |
using Libdl | |
println(Base.Sys.BINDIR) | |
println(Libdl.dlpath(string("lib", splitext(Base.julia_exename())[1]))) | |
println(unsafe_string(Base.JLOptions().image_file)) | |
"""], | |
universal_newlines=True) | |
return out.split("\n")[:3] | |
def demo(): | |
bindir, libjulia_path, image_file = juliainfo() | |
print("Base.Sys.BINDIR =", bindir) | |
print("libjulia_path =", libjulia_path) | |
if not os.path.exists(libjulia_path): | |
raise RuntimeError('Julia library ("libjulia") not found! {}' | |
.format(libjulia_path)) | |
# Handle to libjulia without GIL: | |
dll = ctypes.CDLL(libjulia_path, ctypes.RTLD_GLOBAL) | |
try: | |
jl_init_with_image = dll.jl_init_with_image | |
except AttributeError: | |
try: | |
jl_init_with_image = dll.jl_init_with_image__threading | |
except AttributeError: | |
raise RuntimeError( | |
"No libjulia entrypoint found! (tried jl_init_with_image" | |
" and jl_init_with_image__threading)") | |
jl_init_with_image.argtypes = [c_char_p, c_char_p] | |
jl_init_with_image = jl_init_with_image | |
dll.jl_eval_string.argtypes = [c_char_p] | |
dll.jl_eval_string.restype = c_void_p | |
print("calling jl_init_with_image({}, {})".format(bindir, image_file)) | |
jl_init_with_image(bindir.encode("utf-8"), image_file.encode("utf-8")) | |
print("seems to work...") | |
dll.jl_eval_string(b""" | |
f = let m = Module() | |
Base.eval(m, :( | |
function f(x, y, z) | |
[x, y, z] | |
end | |
)) | |
end | |
""") | |
dll.jl_eval_string(b""" | |
GC.gc() | |
GC.gc() | |
GC.gc() | |
""") | |
if __name__ == "__main__": | |
demo() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment