Class PythonClient
- java.lang.Object
-
- py4j.CallbackClient
-
- py4j.PythonClient
-
- All Implemented Interfaces:
GatewayServerListener,Py4JPythonClient,Py4JPythonClientPerThread
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.
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.List<java.lang.Class<? extends Command>>customCommandsprotected Gatewaygatewayprotected Py4JJavaServerjavaServerprotected java.util.logging.Loggerloggerprotected intreadTimeoutprotected java.lang.ThreadLocal<java.lang.ref.WeakReference<ClientServerConnection>>threadConnection-
Fields inherited from class py4j.CallbackClient
address, authToken, connections, DEFAULT_ADDRESS, DEFAULT_MIN_CONNECTION_TIME, DEFAULT_MIN_CONNECTION_TIME_UNIT, enableMemoryManagement, lock, minConnectionTime, minConnectionTimeUnit, port, socketFactory
-
-
Constructor Summary
Constructors Constructor 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)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidconnectionError(java.lang.Exception e)voidconnectionStarted(Py4JServerConnection gatewayConnection)voidconnectionStopped(Py4JServerConnection gatewayConnection)Py4JPythonClientcopyWith(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 theCallbackClient.minConnectionTimeand theCallbackClient.socketFactory.protected Py4JClientConnectiongetConnection()GatewaygetGateway()Py4JJavaServergetJavaServer()ClientServerConnectiongetPerThreadConnection()intgetReadTimeout()protected voidgiveBackConnection(Py4JClientConnection cc)voidserverError(java.lang.Exception e)This method may be called concurrently with serverPostShutdown().voidserverPostShutdown()This method may be called concurrently with serverStopped() and serverError().voidserverPreShutdown()voidserverStarted()voidserverStopped()This method may be called concurrently with serverPostShutdown().voidsetGateway(Gateway gateway)voidsetJavaServer(Py4JJavaServer javaServer)voidsetPerThreadConnection(ClientServerConnection clientServerConnection)protected voidsetupCleaner()protected booleanshouldRetrySendCommand(Py4JClientConnection cc, Py4JNetworkException pne)protected java.net.SocketstartClientSocket()-
Methods inherited from class py4j.CallbackClient
getAddress, getConnectionLock, getPort, getPythonServerEntryPoint, isMemoryManagementEnabled, periodicCleanup, sendCommand, sendCommand, shutdown
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface py4j.Py4JPythonClient
getAddress, getPort, getPythonServerEntryPoint, isMemoryManagementEnabled, sendCommand, sendCommand, shutdown
-
-
-
-
Field Detail
-
gateway
protected Gateway gateway
-
customCommands
protected java.util.List<java.lang.Class<? extends Command>> customCommands
-
logger
protected final java.util.logging.Logger logger
-
javaServer
protected Py4JJavaServer javaServer
-
threadConnection
protected java.lang.ThreadLocal<java.lang.ref.WeakReference<ClientServerConnection>> threadConnection
-
readTimeout
protected final int readTimeout
-
-
Constructor Detail
-
PythonClient
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)
- Parameters:
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.
-
PythonClient
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)
- Parameters:
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.
-
PythonClient
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)
- Parameters:
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.
-
-
Method Detail
-
getPerThreadConnection
public ClientServerConnection getPerThreadConnection()
- Specified by:
getPerThreadConnectionin interfacePy4JPythonClientPerThread
-
setPerThreadConnection
public void setPerThreadConnection(ClientServerConnection clientServerConnection)
- Specified by:
setPerThreadConnectionin interfacePy4JPythonClientPerThread
-
getGateway
public Gateway getGateway()
- Specified by:
getGatewayin interfacePy4JPythonClientPerThread
-
setGateway
public void setGateway(Gateway gateway)
- Specified by:
setGatewayin interfacePy4JPythonClientPerThread
-
getJavaServer
public Py4JJavaServer getJavaServer()
- Specified by:
getJavaServerin interfacePy4JPythonClientPerThread
-
setJavaServer
public void setJavaServer(Py4JJavaServer javaServer)
- Specified by:
setJavaServerin interfacePy4JPythonClientPerThread
-
getReadTimeout
public int getReadTimeout()
- Specified by:
getReadTimeoutin interfacePy4JPythonClient- Overrides:
getReadTimeoutin classCallbackClient
-
setupCleaner
protected void setupCleaner()
- Overrides:
setupCleanerin classCallbackClient
-
startClientSocket
protected java.net.Socket startClientSocket() throws java.io.IOException- Throws:
java.io.IOException
-
getConnection
protected Py4JClientConnection getConnection() throws java.io.IOException
- Overrides:
getConnectionin classCallbackClient- Throws:
java.io.IOException
-
shouldRetrySendCommand
protected boolean shouldRetrySendCommand(Py4JClientConnection cc, Py4JNetworkException pne)
- Overrides:
shouldRetrySendCommandin classCallbackClient
-
giveBackConnection
protected void giveBackConnection(Py4JClientConnection cc)
- Overrides:
giveBackConnectionin classCallbackClient
-
copyWith
public Py4JPythonClient copyWith(java.net.InetAddress pythonAddress, int pythonPort)
Description copied from class:CallbackClientCreates a callback client which connects to the given address and port, but retains all the other settings (like the
CallbackClient.minConnectionTimeand theCallbackClient.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.- Specified by:
copyWithin interfacePy4JPythonClient- Overrides:
copyWithin classCallbackClient- Parameters:
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.
-
connectionError
public void connectionError(java.lang.Exception e)
- Specified by:
connectionErrorin interfaceGatewayServerListener
-
connectionStarted
public void connectionStarted(Py4JServerConnection gatewayConnection)
- Specified by:
connectionStartedin interfaceGatewayServerListener
-
connectionStopped
public void connectionStopped(Py4JServerConnection gatewayConnection)
- Specified by:
connectionStoppedin interfaceGatewayServerListener
-
serverError
public void serverError(java.lang.Exception e)
Description copied from interface:GatewayServerListenerThis 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.
- Specified by:
serverErrorin interfaceGatewayServerListener
-
serverPostShutdown
public void serverPostShutdown()
Description copied from interface:GatewayServerListenerThis 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.
- Specified by:
serverPostShutdownin interfaceGatewayServerListener
-
serverPreShutdown
public void serverPreShutdown()
- Specified by:
serverPreShutdownin interfaceGatewayServerListener
-
serverStarted
public void serverStarted()
- Specified by:
serverStartedin interfaceGatewayServerListener
-
serverStopped
public void serverStopped()
Description copied from interface:GatewayServerListenerThis 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.
- Specified by:
serverStoppedin interfaceGatewayServerListener
-
-