java - Py4j launch_gateway not connecting properly -
i trying use py4j open gateway can use pass objects java python. when try open gateway py4j function launch_gateway
not seem connect java class. however, when launch java class in command line , connect in python using javagateway
works expected. able use built in method sure not accounting things have been considered in design of py4j, i'm not sure i'm doing wrong.
let's wanted create gateway class sandbox.demo.solver.utilityreporterentrypoint.class
. in command line can executing following:
java -cp /users/grr/anaconda/share/py4j/py4j0.10.4.jar: sandbox.demo.solver.utilityreporterentrypoint py4j.gatewayserver
this launches expected , can use methods in class within python after connecting gateway. far good.
my understanding of py4j documentation lead me believe should following launch gateway in python:
port = launch_gateway(classpath='sandbox.demo.solver.utilityreporterentrypoint') params = gatewayparameters(port=port) gateway= javagateway(gateway_parameters=params)
i no errors when executing these 3 lines, when try access java class methods gateway.entry_point.somemethod()
fails following error:
py4jerror: error occurred while calling t.getreport. trace: py4j.py4jexception: target object id not exist gateway :t @ py4j.gateway.invoke(gateway.java:277) @ py4j.commands.abstractcommand.invokemethod(abstractcommand.java:132) @ py4j.commands.callcommand.execute(callcommand.java:79) @ py4j.gatewayconnection.run(gatewayconnection.java:214) @ java.lang.thread.run(thread.java:745)
obviously not getting called correctly within launch_gateway
or feeding wrong information.
in py4j source code launch_gateway
can see given inputs provide , constructed function, command constructed gets called subprocess.popen
. given input passed launch_gateway
above command passed popen
be:
command = ['java', '-classpath', '/users/grr/anaconda/share/py4j/py4j0.10.4.jar:sandbox.demo.solver.utilityreporterentrypoint', 'py4j.gatewayserver', '0']
passing command popen
returns listening port expected. however, connecting listening port still not allow access class methods.
finally, passing command single string popen without final argument ('0'), launches gateway again operates expected. having taken glance @ java source code py4j.gatewayserver.class makes no sense main method seems indicate class should exit status 1 if length of arguments 0.
at point i'm kind of @ loss. can hack way workable solution, said i'm sure ignores important aspects of gateway behavior , don't hacky solutions. i'd love tag @barthelemy in one, reads this. in advance help.
edit
for have been able work around issue following steps.
package entire project including external dependencies single jar file
magabm-all.jar
, 'main-class' setutilityreporterentrypoint
.include
if...else
block regarding presence of--die-on-exit
ingatewayserver.java
use
subprocess.popen
call command run project jar.
utilityreporterentrypoint.java
public static void main(string[] args) throws ioexception { gatewayserver server = new gatewayserver(new utilityreporterentrypoint()); system.out.println("gateway server started"); server.start(); if (args[0].equals("--die-on-exit")) { try { bufferedreader stdin = new bufferedreader(new inputstreamreader(system.in, charset.forname("utf-8"))); stdin.readline(); system.exit(0); } catch (java.io.ioexception e) { system.exit(1); } } }
app.py
def setup_gateway() """launch py4j gateway using utilityreporterentrypoint.""" process = subprocess.popen('java -jar magabm-all.jar --die-on-exit', shell=true) time.sleep(0.5) gateway = javagateway() return gateway
in way can still use gateway.shutdown
if necessary , if python process starts py4j gateway dies or closed gateway closed.
n.b no means consider final solution py4j written smarter individuals clear purpose in mind , sure there way manage exact workflow within confines of py4j. stopgap solution.
there few issues:
the classpath parameter in
launch_gateway
should directory or jar file, not class name. example, if want include additional java libraries, add them classpath parameter.the error receive when call
gateway.entry_point.somemethod()
means have no entry point. when calllaunch_gateway
, jvm started gatewayserver.main, launches gatewayserver no entry point:gatewayserver server = new gatewayserver(null, port)
. not possible uselaunch_gateway
, specify entry point.when start jvm
java -cp /users/grr/anaconda/share/py4j/py4j0.10.4.jar: sandbox.demo.solver.utilityreporterentrypoint py4j.gatewayserver
believe jvm uses utilityreporterentrypoint main class. although did not provide code, assume class has main method , launches gatewayserver instance of utilityreporterentrypoint entry point. note there whitespace between colon , class name utilityreporterentrypoint seen main class , not being part of classpath.
Comments
Post a Comment