public class PythonClient extends CallbackClient implements Py4JPythonClientPerThread, GatewayServerListener
Subclass of CallbackClient that implements the new threading model, ensuring that each thread uses its own connection.
Modifier and Type | Field and Description |
---|---|
protected java.util.List<java.lang.Class<? extends Command>> |
customCommands |
protected Gateway |
gateway |
protected Py4JJavaServer |
javaServer |
protected java.util.logging.Logger |
logger |
protected int |
readTimeout |
protected java.lang.ThreadLocal<java.lang.ref.WeakReference<ClientServerConnection>> |
threadConnection |
address, authToken, connections, DEFAULT_ADDRESS, DEFAULT_MIN_CONNECTION_TIME, DEFAULT_MIN_CONNECTION_TIME_UNIT, enableMemoryManagement, lock, minConnectionTime, minConnectionTimeUnit, port, socketFactory
Constructor and Description |
---|
PythonClient(Gateway gateway,
java.util.List<java.lang.Class<? extends Command>> customCommands,
int pythonPort,
java.net.InetAddress pythonAddress,
long minConnectionTime,
java.util.concurrent.TimeUnit minConnectionTimeUnit,
javax.net.SocketFactory socketFactory,
Py4JJavaServer javaServer) |
PythonClient(Gateway gateway,
java.util.List<java.lang.Class<? extends Command>> customCommands,
int pythonPort,
java.net.InetAddress pythonAddress,
long minConnectionTime,
java.util.concurrent.TimeUnit minConnectionTimeUnit,
javax.net.SocketFactory socketFactory,
Py4JJavaServer javaServer,
boolean enableMemoryManagement,
int readTimeout) |
PythonClient(Gateway gateway,
java.util.List<java.lang.Class<? extends Command>> customCommands,
int pythonPort,
java.net.InetAddress pythonAddress,
long minConnectionTime,
java.util.concurrent.TimeUnit minConnectionTimeUnit,
javax.net.SocketFactory socketFactory,
Py4JJavaServer javaServer,
boolean enableMemoryManagement,
int readTimeout,
java.lang.String authToken) |
Modifier and Type | Method and Description |
---|---|
void |
connectionError(java.lang.Exception e) |
void |
connectionStarted(Py4JServerConnection gatewayConnection) |
void |
connectionStopped(Py4JServerConnection gatewayConnection) |
Py4JPythonClient |
copyWith(java.net.InetAddress pythonAddress,
int pythonPort)
Creates a callback client which connects to the given address and port,
but retains all the other settings (like the
CallbackClient.minConnectionTime
and the CallbackClient.socketFactory . |
protected Py4JClientConnection |
getConnection() |
Gateway |
getGateway() |
Py4JJavaServer |
getJavaServer() |
ClientServerConnection |
getPerThreadConnection() |
int |
getReadTimeout() |
protected void |
giveBackConnection(Py4JClientConnection cc) |
void |
serverError(java.lang.Exception e)
This method may be called concurrently with serverPostShutdown().
|
void |
serverPostShutdown()
This method may be called concurrently with serverStopped() and
serverError().
|
void |
serverPreShutdown() |
void |
serverStarted() |
void |
serverStopped()
This method may be called concurrently with serverPostShutdown().
|
void |
setGateway(Gateway gateway) |
void |
setJavaServer(Py4JJavaServer javaServer) |
void |
setPerThreadConnection(ClientServerConnection clientServerConnection) |
protected void |
setupCleaner() |
protected boolean |
shouldRetrySendCommand(Py4JClientConnection cc,
Py4JNetworkException pne) |
protected java.net.Socket |
startClientSocket() |
getAddress, getConnectionLock, getPort, getPythonServerEntryPoint, isMemoryManagementEnabled, periodicCleanup, sendCommand, sendCommand, shutdown
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getAddress, getPort, getPythonServerEntryPoint, isMemoryManagementEnabled, sendCommand, sendCommand, shutdown
protected Gateway gateway
protected java.util.List<java.lang.Class<? extends Command>> customCommands
protected final java.util.logging.Logger logger
protected Py4JJavaServer javaServer
protected java.lang.ThreadLocal<java.lang.ref.WeakReference<ClientServerConnection>> threadConnection
protected final int readTimeout
public PythonClient(Gateway gateway, java.util.List<java.lang.Class<? extends Command>> customCommands, int pythonPort, java.net.InetAddress pythonAddress, long minConnectionTime, java.util.concurrent.TimeUnit minConnectionTimeUnit, javax.net.SocketFactory socketFactory, Py4JJavaServer javaServer)
gateway
- The gateway used to pool Java instances created on the Python side.customCommands
- Optional list of custom commands that can be invoked by the Python side.pythonPort
- Port the PythonClient should connect to.pythonAddress
- Address (IP) the PythonClient should connect to.minConnectionTime
- Minimum time to wait before closing unused connections. Not used with PythonClient.minConnectionTimeUnit
- Time unit of minConnectionTimesocketFactory
- SocketFactory used to create a socket.javaServer
- The JavaServer used to receive commands from the Python side.public PythonClient(Gateway gateway, java.util.List<java.lang.Class<? extends Command>> customCommands, int pythonPort, java.net.InetAddress pythonAddress, long minConnectionTime, java.util.concurrent.TimeUnit minConnectionTimeUnit, javax.net.SocketFactory socketFactory, Py4JJavaServer javaServer, boolean enableMemoryManagement, int readTimeout)
gateway
- The gateway used to pool Java instances created on the Python side.customCommands
- Optional list of custom commands that can be invoked by the Python side.pythonPort
- Port the PythonClient should connect to.pythonAddress
- Address (IP) the PythonClient should connect to.minConnectionTime
- Minimum time to wait before closing unused connections. Not used with PythonClient.minConnectionTimeUnit
- Time unit of minConnectionTimesocketFactory
- SocketFactory used to create a socket.javaServer
- The JavaServer used to receive commands from the Python side.enableMemoryManagement
- If false, the Java side does not tell the Python side when a Python proxy is
garbage collected.readTimeout
- Time in milliseconds (0 = infinite). Once connected to the Python side,
if the Java side does not receive a response after this time, the connection with the Python
program is closed. If readTimeout = 0, a default readTimeout of 1000 is used for operations that
must absolutely be non-blocking.public PythonClient(Gateway gateway, java.util.List<java.lang.Class<? extends Command>> customCommands, int pythonPort, java.net.InetAddress pythonAddress, long minConnectionTime, java.util.concurrent.TimeUnit minConnectionTimeUnit, javax.net.SocketFactory socketFactory, Py4JJavaServer javaServer, boolean enableMemoryManagement, int readTimeout, java.lang.String authToken)
gateway
- The gateway used to pool Java instances created on the Python side.customCommands
- Optional list of custom commands that can be invoked by the Python side.pythonPort
- Port the PythonClient should connect to.pythonAddress
- Address (IP) the PythonClient should connect to.minConnectionTime
- Minimum time to wait before closing unused connections. Not used with PythonClient.minConnectionTimeUnit
- Time unit of minConnectionTimesocketFactory
- SocketFactory used to create a socket.javaServer
- The JavaServer used to receive commands from the Python side.enableMemoryManagement
- If false, the Java side does not tell the Python side when a Python proxy is
garbage collected.readTimeout
- Time in milliseconds (0 = infinite). Once connected to the Python side,
if the Java side does not receive a response after this time, the connection with the Python
program is closed. If readTimeout = 0, a default readTimeout of 1000 is used for operations that
must absolutely be non-blocking.authToken
- Token for authenticating with the callback server.public ClientServerConnection getPerThreadConnection()
getPerThreadConnection
in interface Py4JPythonClientPerThread
public void setPerThreadConnection(ClientServerConnection clientServerConnection)
setPerThreadConnection
in interface Py4JPythonClientPerThread
public Gateway getGateway()
getGateway
in interface Py4JPythonClientPerThread
public void setGateway(Gateway gateway)
setGateway
in interface Py4JPythonClientPerThread
public Py4JJavaServer getJavaServer()
getJavaServer
in interface Py4JPythonClientPerThread
public void setJavaServer(Py4JJavaServer javaServer)
setJavaServer
in interface Py4JPythonClientPerThread
public int getReadTimeout()
getReadTimeout
in interface Py4JPythonClient
getReadTimeout
in class CallbackClient
protected void setupCleaner()
setupCleaner
in class CallbackClient
protected java.net.Socket startClientSocket() throws java.io.IOException
java.io.IOException
protected Py4JClientConnection getConnection() throws java.io.IOException
getConnection
in class CallbackClient
java.io.IOException
protected boolean shouldRetrySendCommand(Py4JClientConnection cc, Py4JNetworkException pne)
shouldRetrySendCommand
in class CallbackClient
protected void giveBackConnection(Py4JClientConnection cc)
giveBackConnection
in class CallbackClient
public Py4JPythonClient copyWith(java.net.InetAddress pythonAddress, int pythonPort)
CallbackClient
Creates a callback client which connects to the given address and port,
but retains all the other settings (like the CallbackClient.minConnectionTime
and the CallbackClient.socketFactory
. This method is useful if for some reason
your CallbackServer changes its address or you come to know of the
address after Gateway has already instantiated.
copyWith
in interface Py4JPythonClient
copyWith
in class CallbackClient
pythonAddress
- The address used by a PythonProxyHandler to connect to a
Python gateway.pythonPort
- The port used by a PythonProxyHandler to connect to a Python
gateway. Essentially the port used for Python callbacks.public void connectionError(java.lang.Exception e)
connectionError
in interface GatewayServerListener
public void connectionStarted(Py4JServerConnection gatewayConnection)
connectionStarted
in interface GatewayServerListener
public void connectionStopped(Py4JServerConnection gatewayConnection)
connectionStopped
in interface GatewayServerListener
public void serverError(java.lang.Exception e)
GatewayServerListener
This method may be called concurrently with serverPostShutdown().
Typically a one thread calls shutdown() and then, the thread running the GatewayServer breaks from the connection accept loop.
serverError
in interface GatewayServerListener
public void serverPostShutdown()
GatewayServerListener
This method may be called concurrently with serverStopped() and serverError().
Typically a one thread calls shutdown() and then, the thread running the GatewayServer breaks from the connection accept loop.
serverPostShutdown
in interface GatewayServerListener
public void serverPreShutdown()
serverPreShutdown
in interface GatewayServerListener
public void serverStarted()
serverStarted
in interface GatewayServerListener
public void serverStopped()
GatewayServerListener
This method may be called concurrently with serverPostShutdown().
Typically a one thread calls shutdown() and then, the thread running the GatewayServer breaks from the connection accept loop.
serverStopped
in interface GatewayServerListener