GeniusBridge

class negmas.genius.GeniusBridge[source]

Bases: object

Attributes Summary

gateways

java_processes

python_ports

Methods Summary

clean([port])

Removes all agents and runs garbage collection on the bridge

clean_all()

Removes all agents and runs garbage collection on all bridges.

close_gateway([port])

Closes the gateway.

close_gateways()

Closes all open gateways.

connect([port])

Connects to a running genius-bridge

gateway([port, force])

Finds and returns a gateway for a genius bridge on the given port

is_installed()

Returns true if a geniusbridge.jar is available

is_running(port)

Returns true if a geniusbridge.jar is running on the given port

kill([port, wait])

Kills the java bridge connected to this port by asking it to exit

kill_forced([port, wait])

Kills the java bridge connected to this port forcibly.

kill_threads([port, wait_time])

kills all threads in the given java bridge

restart([port])

Starts or restarts the genius bridge

shutdown([port, wait])

Attempts to shutdown the bridge on that port.

start([port, path, debug, timeout, ...])

Initializes a genius connection

stop([port])

Stops a running bridge

wait_until_listening([port, timeout])

waits until a genius bridge is listening to the given port

wait_until_not_listening([port, timeout])

waits until the genius bridge is not listening to the given port

Attributes Documentation

gateways: dict[int, JavaGateway] = {}
java_processes: dict[int, Any] = {}
python_ports: dict[int, int] = {}

Methods Documentation

classmethod clean(port=25337)[source]

Removes all agents and runs garbage collection on the bridge

Return type:

bool

classmethod clean_all()[source]

Removes all agents and runs garbage collection on all bridges.

Return type:

bool

classmethod close_gateway(port=25337)[source]

Closes the gateway.

Parameters:

port – The port the gateway is connected to. If None, DEFAULT_JAVA_PORT is used.

classmethod close_gateways()[source]

Closes all open gateways.

classmethod connect(port=25337)[source]

Connects to a running genius-bridge

Parameters:

port (int) – The port at which the bridge in listening in Java

Return type:

JavaObject

Remarks:
  • The difference between this method and start() is that this one does not attempt to start a java bridge if one does not exist.

classmethod gateway(port=25337, force=False)[source]

Finds and returns a gateway for a genius bridge on the given port

Parameters:
  • port – The port used by the Jave genius bridge.

  • force – If true, a new gateway is created even if one exists in the list of gateways available in GeniusBridge.gateways.

Return type:

JavaGateway | None

Returns:

The gateway if found otherwise an exception will be thrown

Remarks:
  • this method does NOT start a bridge. It only connects to a running bridge.

classmethod is_installed()[source]

Returns true if a geniusbridge.jar is available

Return type:

bool

classmethod is_running(port)[source]

Returns true if a geniusbridge.jar is running on the given port

Return type:

bool

classmethod kill(port=25337, wait=True)[source]

Kills the java bridge connected to this port by asking it to exit

Return type:

bool

classmethod kill_forced(port=25337, wait=True)[source]

Kills the java bridge connected to this port forcibly.

Return type:

bool

Remarks:
  • The java bridge process must have been started by this process.

classmethod kill_threads(port=25337, wait_time=0.5)[source]

kills all threads in the given java bridge

Return type:

bool

classmethod restart(port=25337, *args, **kwargs)[source]

Starts or restarts the genius bridge

Parameters:

port (int) – port number to use

Return type:

bool

Returns:

True if successful

classmethod shutdown(port=25337, wait=True)[source]

Attempts to shutdown the bridge on that port.

Parameters:

port (int) – The port to shutdown.

Return type:

bool

Remarks:
  • This is the cleanest way to close a java bridge and it simply sends a message to the bridge to shut itself down and cleanly shuts down the py4j bridge.

classmethod start(port=25337, path=None, debug=False, timeout=0, force_timeout=True, save_logs=False, log_path=None, die_on_exit=False, use_shell=False, verbose=False, allow_agent_print=False)[source]

Initializes a genius connection

Parameters:
  • port (int) – port number to use. A value <= 0 means get any free tcp port.

  • path (str | None) – The path to a JAR file that runs negloader

  • debug (bool) – If true, passes –debug to the bridge

  • timeout (float) – If positive and nonzero, passes it as the global timeout for the bridge. Note that currently, the bridge supports only integer timeout values and the fraction will be truncated.

  • force_timeout (bool) – if false, no timeout will be forced by the bridge

  • save_logs (bool) – If false, the brige is instructed not to save any logs

  • log_path (PathLike | None) – the path to store logs from the bridge. Onle effective if save_logs If not given, defaults to ~/negmas/geniusbridge/logs/{port}-{datetime}.txt

  • die_on_exit (bool) – If given, the bridge will be closed when this process is ended

  • use_shell (bool) – If given, the bridge will be started in a subshell.

Return type:

int

Returns:

The port number used by the java process. 0 for failure

Remarks:
  • if a bridge is running, it will return its port and it does not matter whether or not the bridge is started from this process or any other way.

  • it is recommended not to change the defaults for this function.

classmethod stop(port=25337)[source]

Stops a running bridge

Parameters:

port (int) – port number to use

Return type:

bool

Returns:

True if successful

Remarks:
  • You should use this method to stop bridges.

  • It tries the following in order:
    1. shutdown the java bridge by calling its shutdown() method.

    2. killing the java bridge by calling its kill() method.

    3. killing the java bridge forcibly by killing the process

  • This method always waits for a short time to allow each process to complete. If it returns True then the bridge is no longer listening on the given port.

classmethod wait_until_listening(port=25337, timeout=0.5)[source]

waits until a genius bridge is listening to the given port

Parameters:
  • port (int) – The port to test

  • timeout (float) – Maximum time to wait before returning (in seconds)

Return type:

bool

Returns:

True if the genius bridge is running any more (success).

classmethod wait_until_not_listening(port=25337, timeout=0.5)[source]

waits until the genius bridge is not listening to the given port

Parameters:
  • port (int) – The port to test

  • max_sleep – Maximum time to wait before returning (in seconds)

Return type:

bool

Returns:

True if the genius bridge is NOT running any more (success).