Class PythonClient
- All Implemented Interfaces:
GatewayServerListener
,Py4JPythonClient
,Py4JPythonClientPerThread
Subclass of CallbackClient that implements the new threading model, ensuring that each thread uses its own connection.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected Gateway
protected Py4JJavaServer
protected final Logger
protected final int
protected ThreadLocal<WeakReference<ClientServerConnection>>
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
ConstructorsConstructorDescriptionPythonClient
(Gateway gateway, List<Class<? extends Command>> customCommands, int pythonPort, InetAddress pythonAddress, long minConnectionTime, TimeUnit minConnectionTimeUnit, SocketFactory socketFactory, Py4JJavaServer javaServer) PythonClient
(Gateway gateway, List<Class<? extends Command>> customCommands, int pythonPort, InetAddress pythonAddress, long minConnectionTime, TimeUnit minConnectionTimeUnit, SocketFactory socketFactory, Py4JJavaServer javaServer, boolean enableMemoryManagement, int readTimeout) PythonClient
(Gateway gateway, List<Class<? extends Command>> customCommands, int pythonPort, InetAddress pythonAddress, long minConnectionTime, TimeUnit minConnectionTimeUnit, SocketFactory socketFactory, Py4JJavaServer javaServer, boolean enableMemoryManagement, int readTimeout, String authToken) -
Method Summary
Modifier and TypeMethodDescriptionvoid
void
connectionStarted
(Py4JServerConnection gatewayConnection) void
connectionStopped
(Py4JServerConnection gatewayConnection) copyWith
(InetAddress pythonAddress, int pythonPort) Creates a callback client which connects to the given address and port, but retains all the other settings (like theCallbackClient.minConnectionTime
and theCallbackClient.socketFactory
.protected Py4JClientConnection
int
protected void
void
This method may be called concurrently with serverPostShutdown().void
This method may be called concurrently with serverStopped() and serverError().void
void
void
This method may be called concurrently with serverPostShutdown().void
setGateway
(Gateway gateway) void
setJavaServer
(Py4JJavaServer javaServer) void
setPerThreadConnection
(ClientServerConnection clientServerConnection) protected void
protected boolean
protected Socket
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 Details
-
gateway
-
customCommands
-
logger
-
javaServer
-
threadConnection
-
readTimeout
protected final int readTimeout
-
-
Constructor Details
-
PythonClient
public PythonClient(Gateway gateway, List<Class<? extends Command>> customCommands, int pythonPort, InetAddress pythonAddress, long minConnectionTime, TimeUnit minConnectionTimeUnit, 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, List<Class<? extends Command>> customCommands, int pythonPort, InetAddress pythonAddress, long minConnectionTime, TimeUnit minConnectionTimeUnit, 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, List<Class<? extends Command>> customCommands, int pythonPort, InetAddress pythonAddress, long minConnectionTime, TimeUnit minConnectionTimeUnit, SocketFactory socketFactory, Py4JJavaServer javaServer, boolean enableMemoryManagement, int readTimeout, 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 Details
-
getPerThreadConnection
- Specified by:
getPerThreadConnection
in interfacePy4JPythonClientPerThread
-
setPerThreadConnection
- Specified by:
setPerThreadConnection
in interfacePy4JPythonClientPerThread
-
getGateway
- Specified by:
getGateway
in interfacePy4JPythonClientPerThread
-
setGateway
- Specified by:
setGateway
in interfacePy4JPythonClientPerThread
-
getJavaServer
- Specified by:
getJavaServer
in interfacePy4JPythonClientPerThread
-
setJavaServer
- Specified by:
setJavaServer
in interfacePy4JPythonClientPerThread
-
getReadTimeout
public int getReadTimeout()- Specified by:
getReadTimeout
in interfacePy4JPythonClient
- Overrides:
getReadTimeout
in classCallbackClient
-
setupCleaner
protected void setupCleaner()- Overrides:
setupCleaner
in classCallbackClient
-
startClientSocket
- Throws:
IOException
-
getConnection
- Overrides:
getConnection
in classCallbackClient
- Throws:
IOException
-
shouldRetrySendCommand
- Overrides:
shouldRetrySendCommand
in classCallbackClient
-
giveBackConnection
- Overrides:
giveBackConnection
in classCallbackClient
-
copyWith
Description copied from class:CallbackClient
Creates a callback client which connects to the given address and port, but retains all the other settings (like the
CallbackClient.minConnectionTime
and 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:
copyWith
in interfacePy4JPythonClient
- Overrides:
copyWith
in 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
- Specified by:
connectionError
in interfaceGatewayServerListener
-
connectionStarted
- Specified by:
connectionStarted
in interfaceGatewayServerListener
-
connectionStopped
- Specified by:
connectionStopped
in interfaceGatewayServerListener
-
serverError
Description copied from interface: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.
- Specified by:
serverError
in interfaceGatewayServerListener
-
serverPostShutdown
public void serverPostShutdown()Description copied from interface: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.
- Specified by:
serverPostShutdown
in interfaceGatewayServerListener
-
serverPreShutdown
public void serverPreShutdown()- Specified by:
serverPreShutdown
in interfaceGatewayServerListener
-
serverStarted
public void serverStarted()- Specified by:
serverStarted
in interfaceGatewayServerListener
-
serverStopped
public void serverStopped()Description copied from interface: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.
- Specified by:
serverStopped
in interfaceGatewayServerListener
-