Skip to content

Is juliacall supporting python multiprocessing? #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
markNZed opened this issue Dec 5, 2021 · 8 comments
Closed

Is juliacall supporting python multiprocessing? #77

markNZed opened this issue Dec 5, 2021 · 8 comments

Comments

@markNZed
Copy link

markNZed commented Dec 5, 2021

I would like to use juliacall in a python script that is using multiprocessing. So far I get seg faults. Is there an example of how to do this ? Thanks

@cjdoris
Copy link
Collaborator

cjdoris commented Dec 5, 2021

I've never tried multiprocessing but there's no obvious reason it shouldn't work. Can you please post some minimal code that crashes and the full error message?

@markNZed
Copy link
Author

markNZed commented Dec 5, 2021

It does seem to be working. I needed to import inside each of the worker process e.g.

    def initialize(self):
        from juliacall import Main as jl
        jl.seval("using Pkg; Pkg.activate(\"..\"); Pkg.instantiate(); using ARTime")

@cjdoris
Copy link
Collaborator

cjdoris commented Dec 5, 2021

Great!

@cjdoris cjdoris closed this as completed Dec 5, 2021
@VietTralala
Copy link

VietTralala commented Feb 14, 2023

For me this does not work. I can call jl.seval from the main process however in the worker process I always get the following error and the worker process crashes

python: /buildworker/worker/package_linux64/build/src/debuginfo.cpp:1634: void register_eh_frames(uint8_t*, size_t): Assertion `end_ip != 0' failed.

Without the multiprocessing Pool I can call the julia script just fine. What can I do to further debug this?

Here is my minimal example:

import os
os.environ['PYTHON_JULIAPKG_EXE'] = "/home/user/.juliaup/bin/julia"
os.environ['PYTHON_JULIAPKG_OFFLINE'] = 'yes'
os.environ['PYTHON_JULIAPKG_PROJECT'] = '/home/user/julia/environments/v1.6/'

from juliacall import Main as jl, convert as jlconvert

from multiprocessing import Pool
from tqdm import tqdm

import ipdb

def init_worker():
    import os
    os.environ['PYTHON_JULIAPKG_EXE'] = "/home/user/juliaup/bin/julia"
    os.environ['PYTHON_JULIAPKG_OFFLINE'] = 'yes'
    os.environ['PYTHON_JULIAPKG_PROJECT'] = '/home/user/.julia/environments/v1.6/'

    from juliacall import Main as jl, convert as jlconvert

    print('in init_worker()...')
    jl.seval('using Pkg')
    jl.seval('Pkg.status()')
    print('...done')

def compute(jobid):
    print(f'in main({jobid})...')

    jl.seval('include("test_julia_simple.jl")')
    print('...done')
    return

def main():
    njobs = 10
    #start pool with init_worker() as initializer
    
    with Pool(2, initializer=init_worker) as p, tqdm(total=njobs) as pbar:
        res = []
        for jid in range(njobs):
            res.append(p.apply_async(compute, (jid,)))
        for r in res:
            r.get()
            pbar.update(1)


if __name__ == "__main__":
    main()

And the julia script test_julia_simple.jl

for i in 1:10
    println(i)
end
1+2

additional info:

$ python --version
Python 3.9.7
$ pip freeze | grep julia
juliacall==0.9.10
juliapkg==0.1.9

$ julia --version
The latest version of Julia in the `1.6` channel is 1.6.7+0.x64.linux.gnu. You currently have `1.6.6+0~x64` installed. Run:                           

  juliaup update

to install Julia 1.6.7+0.x64.linux.gnu and update the `1.6` channel to that version.                                                                  
julia version 1.6.6

not sure if this is related but the error message is nearly identical
JuliaLang/julia#44969

@cjdoris
Copy link
Collaborator

cjdoris commented Feb 15, 2023

I imagine you need to import juliacall within the compute function too.

@VietTralala
Copy link

I imagine you need to import juliacall within the compute function too.

I tried what you suggested but the script still fails with the same error message.

# the new compute function with inner imports:
def compute(jobid):
    import os
    os.environ['PYTHON_JULIAPKG_EXE'] = "/system/user/tran/.juliaup/bin/julia"
    os.environ['PYTHON_JULIAPKG_OFFLINE'] = 'yes'
    os.environ['PYTHON_JULIAPKG_PROJECT'] = '/system/user/tran/.julia/environments/v1.6/'
    from juliacall import Main as jl, convert as jlconvert

    print(f'in main({jobid})...')

    jl.seval('include("test_julia_simple.jl")')
    print('...done')
    return

@markNZed
Copy link
Author

markNZed commented Feb 17, 2023

I did get this working but it was quite some time ago. One option would be to strip down what I did and use that as a "working" starting point to figure out what the difference in your situation is. You can find the details on the package I built at https://github.com/markNZed/ARTimeNAB.jl and the code dealing with difference processes is over here https://github.com/numenta/NAB/blob/master/nab/detectors/ARTime/ARTime_detector.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants