Skip to content
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

Question - it's possible to use jpype with multiprocessing #533

Closed
dadokkio opened this issue Nov 4, 2019 · 9 comments
Closed

Question - it's possible to use jpype with multiprocessing #533

dadokkio opened this issue Nov 4, 2019 · 9 comments

Comments

@dadokkio
Copy link
Contributor

dadokkio commented Nov 4, 2019

Hi,
I'm trying to parallelize some jpype code using multiprocessing but I'm not sure if this is possible and if this is the correct way to proceed.

import itertools
import jpype.imports
from jpype import JString
from glob import glob
from multiprocessing import cpu_count, Pool

def write_data(Q):
    (J_Class, condition, parameter) = Q  
    items = J_Class.read(condition, parameter)
    print(len(items.getData()), condition, parameter)

classpath = ":".join(glob("LIB/*.jar"))
jpype.startJVM(jpype.getDefaultJVMPath(), "-Djava.class.path=%s" % classpath, convertStrings=True)
J_API = jpype.JClass("example.class")
J_Class = J_API(JString('filepath'))
param_1 = [int(x) for x in J_Class.getParam1List()]
param_2 = [int(x) for x in J_Class.getParam2List()]

with Pool(cpu_count()) as pool:
    pool.map(write_data, [(J_Class, x, y) for (x, y) in itertools.product(param_1, param_2)])

In this way multiprocessing is not able to pickle the class so I tried to initialize the class directly in the write_data function:

def write_data(Q):
    (condition, parameter) = Q  
    logging.warning(f"This is printed")
    J_API = jpype.JClass("example.class")
    logging.warning(f"This is not printed")
    J_Class = J_API(JString('filepath'))
    items = J_Class.read(condition, parameter)
    print(len(items.getData()), condition, parameter)

with Pool(cpu_count()) as pool:
    pool.map(write_data, [(x, y) for (x, y) in itertools.product(param_1, param_2)])

With this second method I can see all processes spawned but then nothing happens.
Any help?

@Thrameos
Copy link
Contributor

Thrameos commented Nov 4, 2019 via email

@dadokkio
Copy link
Contributor Author

dadokkio commented Nov 5, 2019

Ok, I'm trying to build newer release from source but now I've new questions :)

  • is master the right branch? Is more updated than devel but both seems to be release 0.7.0
  • the build is ok, but some test are failing [15 failed, 432 passed, 40 skipped, 2 warnings in 5.93s], Can I ignore them? Do you need the log?

In any case now I've jpype.pickle working but multiprocessing is not able to serialize the class like before.. I need to specify something?

Thanks

@Thrameos
Copy link
Contributor

Thrameos commented Nov 5, 2019 via email

@dadokkio
Copy link
Contributor Author

dadokkio commented Nov 6, 2019

I've update the log in this gist: https://gist.github.com/dadokkio/608a6322f2b0fa7054bca83e11ada195
Some machine and setup info:
`[root@xxx1 jpype-master]# ant -v
Apache Ant(TM) version 1.9.2 compiled on January 22 2014

[root@xxx1 jpype-master]# echo $JAVA_HOME
/usr/java/jdk1.8.0_131

[root@xxx1 jpype-master]# python --version
Python 3.7.5

[root@xxx1 jpype-master]# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 7.2 (Maipo)
`

@Thrameos
Copy link
Contributor

Thrameos commented Nov 7, 2019 via email

@dadokkio
Copy link
Contributor Author

dadokkio commented Nov 8, 2019

Ok, that's worked!
449 passed, 38 skipped, 2 warnings in 5.70s

But I've the same situation than before:

_pickle.PicklingError: Can't pickle <java class 'example.classes.JFA_API'>: attribute lookup example.classes.JFA_API on jpype._jclass failed

Do I need to specify the pickling class somewhere?

@Thrameos
Copy link
Contributor

Thrameos commented Nov 8, 2019 via email

@dadokkio
Copy link
Contributor Author

Ok, I was able to use the class with getstate and setstate but my problem seems to be related to the class itself because it returns:
jpype._jclass.NotSerializableException: java.io.NotSerializableException

@Thrameos
Copy link
Contributor

Thrameos commented Nov 11, 2019 via email

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

2 participants