The py4j.java_gateway module defines most of the classes that are needed to use Py4J. Py4J users are expected to only use explicitly JavaGateway and optionally, GatewayClient, java_import, get_field, and get_method. The other module members are documented to support the extension of Py4J.
A JavaGateway is the main interaction point between a Python VM and a JVM.
Methods that are not defined by JavaGateway are always redirected to entry_point. For example, gateway.doThat() is equivalent to gateway.entry_point.doThat(). This is a trade-off between convenience and potential confusion.
Parameters: |
|
---|
Closes all gateway connections. A connection will be reopened if necessary (e.g., if a JavaMethod is called).
Parameters: |
|
---|
Makes the Java Gateway dereference this object.
The equivalent of this method is called when a JavaObject instance is garbage collected on the Python side. This method, or gc.collect() should still be invoked when memory is limited or when too many objects are created on the Java side.
Parameters: |
|
---|
Displays a help page about a class or an object.
Parameters: |
|
---|
Creates a Java array of type java_class of dimensions
Parameters: |
|
---|---|
Return type: | A JavaArray instance. |
Creates a new JVM view with its own imports. A JVM view ensures that the import made in one view does not conflict with the import of another view.
Generally, each Python module should have its own view (to replicate Java behavior).
Parameters: |
|
---|---|
Return type: | A JVMView instance (same class as the gateway.jvm instance). |
Shuts down the callback server (if started) and restarts a new one.
Shuts down the GatewayClient and the CallbackServer.
Using the jvm property:
>>> gateway = JavaGateway()
>>> jvm = gateway.jvm
>>> l = jvm.java.util.ArrayList()
>>> l.append(10)
>>> l.append(1)
>>> jvm.java.util.Collections.sort(l)
>>> l
[1, 10]
>>> l.append(5)
>>> l.sort()
>>> l
[1, 5, 10]
Using auto_field:
First we declare a class that has a field AND a method called member:
package py4j.examples;
public class ExampleWithField {
public int member = 1;
public String member() {
return "Hello World";
}
}
Then we play with the class using the two possible values of auto_field:
>>> java_gateway = JavaGateway() # auto_field = False
>>> example = java_gateway.jvm.py4j.examples.ExampleWithField()
>>> example.member()
u'Hello World'
>>> get_field(example,'member')
1
>>> java_gateway2 = JavaGateway(auto_field=True)
>>> example2 = java_gateway2.jvm.py4j.examples.ExampleWithField()
>>> example2.member
1
>>> get_method(example2,'member')()
u'Hello World'
Responsible for managing connections to the JavaGateway.
This implementation is thread-safe and connections are created on-demand. This means that Py4J-Python can be accessed by multiple threads and messages are sent to and processed concurrently by the Java Gateway.
When creating a custom JavaGateway, it is recommended to pass an instance of GatewayClient instead of a GatewayConnection: both have the same interface, but the client supports multiple threads and connections, which is essential when using callbacks.
Parameters: |
|
---|
Closes all currently opened connections.
This operation is not thread safe and is only a best effort strategy to close active connections. All connections are guaranteed to be closed only if no other thread is accessing the client and no call is pending.
Sends a command to the JVM. This method is not intended to be called directly by Py4J users: it is usually called by JavaMember instances.
Parameters: |
|
---|---|
Retry : | if True, the GatewayClient tries to resend a message if it fails. |
Return type: | the string answer received from the JVM. The answer follows the Py4J protocol. |
Sends a shutdown command to the gateway. This will close the gateway server: all active connections will be closed. This may be useful if the lifecycle of the Java program must be tied to the Python program.
Default gateway connection (socket based) responsible for communicating with the Java Virtual Machine.
Parameters: |
|
---|
Closes the connection by closing the socket.
Sends a command to the JVM. This method is not intended to be called directly by Py4J users: it is usually called by JavaMember instances.
Parameters: |
|
---|---|
Return type: | the string answer received from the JVM. The answer follows the Py4J protocol. |
Sends a shutdown command to the gateway. This will close the gateway server: all active connections will be closed. This may be useful if the lifecycle of the Java program must be tied to the Python program.
Starts the connection by connecting to the address and the port
A JVMView allows access to the Java Virtual Machine of a JavaGateway. This can be used to reference static members (fields and methods) and to call constructors.
Represents a Java object from which you can call methods or access fields.
Parameters: |
|
---|
Represents a member (i.e., method) of a JavaObject. For now, only methods are supported. Fields are retrieved directly and are not contained in a JavaMember.
A JavaClass represents a Java Class from which static members can be retrieved. JavaClass instances are also needed to initialize an array.
Usually, JavaClass are not initialized using their constructor, but they are created while accessing the jvm property of a gateway, e.g., gateway.jvm.java.lang.String.
A JavaPackage represents part of a Java package from which Java classes can be accessed.
Usually, JavaPackage are not initialized using their constructor, but they are created while accessing the jvm property of a gateway, e.g., gateway.jvm.java.lang.
A PythonProxyPool manages proxies that are passed to the Java side. A proxy is a Python class that implements a Java interface.
A proxy has an internal class named Java with a member named implements which is a list of fully qualified names (string) of the implemented interfaces.
The PythonProxyPool implements a subset of the dict interface: pool[id], del(pool[id]), pool.put(proxy), pool.clear(), id in pool, len(pool).
The PythonProxyPool is thread-safe.
Adds a proxy to the pool.
Parameters: |
|
---|---|
Return type: | A unique identifier associated with the object. |
The CallbackServer is responsible for receiving call back connection requests from the JVM. Usually connections are reused on the Java side, but there is at least one connection per concurrent thread.
Parameters: |
|
---|
Starts listening and accepting connection requests.
This method is called when invoking CallbackServer.start(). A CallbackServer instance is created and started automatically when a JavaGateway instance is created.
Stops listening and accepting connection requests. All live connections are closed.
This method can safely be called by another thread.
The following functions get be used to import packages or to get a particular field or method when fields and methods in a Java class have the same name:
Imports the package or class specified by import_str in the jvm view namespace.
Parameters: |
|
---|---|
Import_str : | The class (e.g., java.util.List) or the package (e.g., java.io.*) to import |
Retrieves the field named field_name from the java_object.
This function is useful when auto_field=false in a gateway or Java object.
Parameters: |
|
---|
Sets the field named field_name of java_object to value.
This function is the only way to set a field because the assignment operator in Python cannot be overloaded.
Parameters: |
|
---|
Retrieves a reference to the method of an object.
This function is useful when auto_field=true and an instance field has the same name as a method. The full signature of the method is not required: it is determined when the method is called.
Parameters: |
|
---|