1

I want to execute a code in Julia calling it from python. When I run it locally on my machine, it works fine, but when I try to run it with flask, it returns an error and stops the execution.

Here is an example of a Julia code (example.jl) being calld from python via flask:

function echomsg(msg)
    return msg 
end

The python code is as follows

from flask import Flask
import julia
from julia import Julia
app = Flask(__name__)
@ app.route("/")
def main():
    # j = julia.Julia()
    # j.include("example.jl")
    for l in "blabla":
        Julia(compiled_modules=False)
        j = julia.Julia("example.jl")
        j.include("example.jl")
        print(j.echomsg(l))
    return 'OK'
if __name__ == "__main__":
    app.run('0.0.0.0',port=8080)

​It prints "blabla" but then stops the process and returns the following error:

Traceback (most recent call last):
  File "C:\Users\Pedro\Documents\floki\allocation\app.py", line 250, in <module>
    app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 8080)))
  File "C:\Users\Pedro\.virtualenvs\allocation-enVgDhcs\lib\site-packages\flask\app.py", line 990, in run
    run_simple(host, port, self, **options)
  File "C:\Users\Pedro\.virtualenvs\allocation-enVgDhcs\lib\site-packages\werkzeug\serving.py",
line 1050, in run_simple
    run_with_reloader(inner, extra_files, reloader_interval, reloader_type)
  File "C:\Users\Pedro\.virtualenvs\allocation-enVgDhcs\lib\site-packages\werkzeug\_reloader.py", line 337, in run_with_reloader
    reloader.run()
  File "C:\Users\Pedro\.virtualenvs\allocation-enVgDhcs\lib\site-packages\werkzeug\_reloader.py", line 202, in run
    for filename in chain(_iter_module_files(), self.extra_files):
  File "C:\Users\Pedro\.virtualenvs\allocation-enVgDhcs\lib\site-packages\werkzeug\_reloader.py", line 24, in _iter_module_files
    filename = getattr(module, "__file__", None)
  File "C:\Users\Pedro\.virtualenvs\allocation-enVgDhcs\lib\site-packages\julia\core.py", line 176, in __getattr__
    return self.__try_getattr(name)
  File "C:\Users\Pedro\.virtualenvs\allocation-enVgDhcs\lib\site-packages\julia\core.py", line 189, in __try_getattr
    if self._julia.isamodule(jl_fullname):
  File "C:\Users\Pedro\.virtualenvs\allocation-enVgDhcs\lib\site-packages\julia\core.py", line 660, in isamodule
    return self.eval("isa({}, Module)".format(julia_name))
  File "C:\Users\Pedro\.virtualenvs\allocation-enVgDhcs\lib\site-packages\julia\core.py", line 605, in eval
    ans = self._call(src)
  File "C:\Users\Pedro\.virtualenvs\allocation-enVgDhcs\lib\site-packages\julia\core.py", line 537, in _call
    ans = self.api.jl_eval_string(src.encode('utf-8'))
OSError: exception: access violation reading 0x00000000000019A8
Error in atexit._run_exitfuncs:
OSError: exception: access violation reading 0x00000000000019A0

How can I solve it?

donut
  • 628
  • 2
  • 9
  • 23
  • Could it be that the Flask is using another virtual env than the one you use for tests? Julia needs to have PyCall build with exactly the same PYthon that you use to call Julia from Python. If they do not match this is what usually could happen. See also: https://stackoverflow.com/questions/64241264/i-have-a-high-performant-function-written-in-julia-how-can-i-use-it-from-python – Przemyslaw Szufel Jan 24 '21 at 18:37

0 Answers0