def setUp(self): self.p = start_example_app_process3() self.gateway = JavaGateway( callback_server_parameters=CallbackServerParameters( eager_load=False))
def setUp(self): self.p = start_example_app_process() self.gateway = JavaGateway( callback_server_parameters=CallbackServerParameters( propagate_java_exceptions=True)) sleep()
from py4j.java_gateway import JavaGateway, CallbackServerParameters class PythonListener(object): def __init__(self, gateway): self.gateway = gateway def notify(self, obj): print("Notified by Java") print(obj) gateway.jvm.System.out.println("Hello from python!") return "A Return Value" class Java: implements = ["py4j.examples.ExampleListener"] if __name__ == "__main__": gateway = JavaGateway( callback_server_parameters=CallbackServerParameters()) listener = PythonListener(gateway) gateway.entry_point.registerListener(listener) gateway.entry_point.notifyAllListeners() gateway.shutdown()
def setUp(self): self.p = start_example_app_process2() self.gateway = JavaGateway( callback_server_parameters=CallbackServerParameters())
self.error += math.fabs((1.0 if self.expected else 0.0) - output[0]) if (output[0] >= 0.5) == self.expected: self.correct += 1 def getScore(self): return (4.0 - self.error) ** 2.0 def isWinner(self): return self.correct == 4 class Java: implements = ['plu.teamtwo.rtm.neat.ScoringFunction'] if __name__ == '__main__': gateway = JavaGateway(callback_server_parameters=CallbackServerParameters()) gateway.entry_point.init(gateway.jvm.plu.teamtwo.rtm.neat.Encoding.DIRECT_ENCODING, 3, 1) controller = gateway.entry_point.getController() controller.createFirstGeneration() for _ in range(0, 500): found_winner = controller.assesGeneration(XORScorePy()) best = controller.getBestIndividual() print('Gen {:d}: {:.2f}, {:.1f}' .format(controller.getGenerationNum(), controller.getFitness(), best.getFitness())) # print('generation') if found_winner: gson = gateway.jvm.com.google.gson.GsonBuilder().setPrettyPrinting().create() print(gson.toJson(best)) break
# almost the same code can be used for Python and Jython. # In this example, we perform all the steps # that are usually handled by connect2y # for illustraition/testing purposes. try: # Fetch Py4Y port used by display from command line port = int(sys.argv[1]) f.write("Invoked with port %d\n" % port) from py4j.java_gateway import JavaGateway, GatewayParameters, CallbackServerParameters # Connect python side to Java, # start python callback server with a dynamic port gateway = JavaGateway( gateway_parameters=GatewayParameters(port=port), callback_server_parameters=CallbackServerParameters(port=0)) # Retrieve the port to which the python callback server was bound python_port = gateway.get_callback_server().get_listening_port() # Tell the Java side to connect to the python callback server with the new # python port, using the java_gateway_server attribute that retrieves the # GatewayServer instance addr = gateway.java_gateway_server.getCallbackClient().getAddress() gateway.java_gateway_server.resetCallbackClient(addr, python_port) # Fetch proxy objects that allow interaction with display map = gateway.getMap() for var in map: f.write("Received from Java: %s\n" % var) # Map should contain at least these:
global GAME_NUM GAME_NUM = int(args[i+1]) def start_game(): manager.registerAI("KickAIPython", KickAI(gateway)) print("Start game") game = manager.createGame("ZEN", "ZEN", "KickAIPython", "RandomAI", GAME_NUM) manager.runGame(game) print("After game") sys.stdout.flush() def close_gateway(): gateway.close_callback_server() gateway.close() def main_process(): check_args(args) start_game() close_gateway() args = sys.argv argc = len(args) GAME_NUM = 2 gateway = JavaGateway(gateway_parameters=GatewayParameters(port=4242), callback_server_parameters=CallbackServerParameters()); manager = gateway.entry_point main_process()
import logging from py4j.java_gateway import JavaGateway, CallbackServerParameters logging.basicConfig(level=logging.INFO) #TODO: Add JVM Loader Code _java_gateway = JavaGateway( callback_server_parameters=CallbackServerParameters( daemonize=True, daemonize_connections=True)) logging.info("Java Gateway Established")
def __init__(self): self._is_closed = False # TODO: Do not start multiple JVMs self._process = Context._start_jvm_process() while self._process.poll() is None: msg = self._process.stdout.readline() try: msg = json.loads(msg) if 'status' in msg and msg['status'] == 'ready': self.status = Context.STATUS_READY elif 'status' in msg and msg['status'] == 'failed': self.status = Context.STATUS_ADDRESS_IN_USE raise Exception('Address already in use.') break except json.JSONDecodeError: continue self._gateway = JavaGateway( callback_server_parameters=CallbackServerParameters()) for name, package in (('game', 'net.demilich.metastone.game.*'), ('entities', 'net.demilich.metastone.game.entities.*'), ('decks', 'net.demilich.metastone.game.decks.*'), ('events', 'net.demilich.metastone.game.events.*'), ('actions', 'net.demilich.metastone.game.actions.*'), ('logic', 'net.demilich.metastone.game.logic.*'), ('cards', 'net.demilich.metastone.game.cards.*'), ('spells', 'net.demilich.metastone.game.spells.*'), ('targeting', 'net.demilich.metastone.game.targeting.*'), ('utils', 'net.demilich.metastone.game.utils.*'), ('behaviour', 'net.demilich.metastone.game.behaviour.*')): view = self._gateway.new_jvm_view(name) java_import(view, package) setattr(self, name, view) # Include the important classes and enums self.GameAction = self.actions.GameAction self.Card = self.cards.CardCatalogue self.Deck = self.decks.Deck self.Entity = self.entities.Entity self.Actor = self.entities.Actor self.GameEvent = self.events.GameEvent self.GameEventType = self.game.events.GameEventType self.EntityType = self.entities.EntityType self.Weapon = self.entities.Weapons.Weapon self.Minion = self.entities.minions.Minion self.Hero = self.entities.heroes.Hero self.Attribute = self.utils.Attribute self.ActionType = self.actions self.Rarity = self.cards.Rarity self.CardType = self.cards.CardType self.CardSet = self.cards.CardSet self.GameLogic = self.logic.GameLogic self.Zones = self.targeting.Zones self.HeroClass = self.entities.heroes.HeroClass self.CardCatalogue = self.cards.CardCatalogue self.PythonBridge = self._gateway.jvm.com.hiddenswitch.spellsource.applications.PythonBridge self.ArrayList = self._gateway.jvm.java.util.ArrayList self.CardCatalogue.loadCardsFromPackage()
def main(): sys_stdin = sys.stdin sys_stdout = sys.stdout sys_stderr = sys.stderr if sys.version >= '3': sys.stdin = io.StringIO() else: sys.stdin = cStringIO.StringIO() sys.stdout = UnicodeDecodingStringIO() sys.stderr = UnicodeDecodingStringIO() spark_major_version = os.getenv("LIVY_SPARK_MAJOR_VERSION") try: listening_port = 0 if os.environ.get("LIVY_TEST") != "true": #Load spark into the context exec('from pyspark.shell import sc', global_dict) exec('from pyspark.shell import sqlContext', global_dict) exec('from pyspark.sql import HiveContext', global_dict) exec('from pyspark.streaming import StreamingContext', global_dict) exec('import pyspark.cloudpickle as cloudpickle', global_dict) if spark_major_version >= "2": exec('from pyspark.shell import spark', global_dict) else: # LIVY-294, need to check whether HiveContext can work properly, # fallback to SQLContext if HiveContext can not be initialized successfully. # Only for spark-1. code = textwrap.dedent(""" import py4j from pyspark.sql import SQLContext try: sqlContext.tables() except py4j.protocol.Py4JError: sqlContext = SQLContext(sc)""") exec(code, global_dict) #Start py4j callback server from py4j.protocol import ENTRY_POINT_OBJECT_ID from py4j.java_gateway import JavaGateway, GatewayClient, CallbackServerParameters gateway_client_port = int(os.environ.get("PYSPARK_GATEWAY_PORT")) gateway = JavaGateway(GatewayClient(port=gateway_client_port)) gateway.start_callback_server( callback_server_parameters=CallbackServerParameters(port=0)) socket_info = gateway._callback_server.server_socket.getsockname() listening_port = socket_info[1] pyspark_job_processor = PySparkJobProcessorImpl() gateway.gateway_property.pool.dict[ ENTRY_POINT_OBJECT_ID] = pyspark_job_processor global local_tmp_dir_path, job_context local_tmp_dir_path = tempfile.mkdtemp() job_context = JobContextImpl() print(sys.stdout.getvalue(), file=sys_stderr) print(sys.stderr.getvalue(), file=sys_stderr) clearOutputs() print('READY(port=' + str(listening_port) + ')', file=sys_stdout) sys_stdout.flush() while True: line = sys_stdin.readline() if line == '': break elif line == '\n': continue try: msg = json.loads(line) except ValueError: LOG.error('failed to parse message', exc_info=True) continue try: msg_type = msg['msg_type'] except KeyError: LOG.error('missing message type', exc_info=True) continue try: content = msg['content'] except KeyError: LOG.error('missing content', exc_info=True) continue if not isinstance(content, dict): LOG.error('content is not a dictionary') continue try: handler = msg_type_router[msg_type] except KeyError: LOG.error('unknown message type: %s', msg_type) continue response = handler(content) try: response = json.dumps(response) except ValueError: response = json.dumps({ 'msg_type': 'inspect_reply', 'content': { 'status': 'error', 'ename': 'ValueError', 'evalue': 'cannot json-ify %s' % response, 'traceback': [], } }) print(response, file=sys_stdout) sys_stdout.flush() finally: if os.environ.get("LIVY_TEST") != "true" and 'sc' in global_dict: gateway.shutdown_callback_server() shutil.rmtree(local_tmp_dir_path) global_dict['sc'].stop() sys.stdin = sys_stdin sys.stdout = sys_stdout sys.stderr = sys_stderr
def launch_gateway(): # type: () -> JavaGateway """ launch jvm gateway """ if is_launch_gateway_disabled(): raise Exception( "It's launching the PythonGatewayServer during Python UDF execution " "which is unexpected. It usually happens when the job codes are " "in the top level of the Python script file and are not enclosed in a " "`if name == 'main'` statement.") FLINK_HOME = _find_flink_home() # TODO windows support on_windows = platform.system() == "Windows" if on_windows: raise Exception("Windows system is not supported currently.") script = "./bin/pyflink-gateway-server.sh" command = [os.path.join(FLINK_HOME, script)] command += ['-c', 'org.apache.flink.client.python.PythonGatewayServer'] submit_args = os.environ.get("SUBMIT_ARGS", "local") command += shlex.split(submit_args) # Create a temporary directory where the gateway server should write the connection information. conn_info_dir = tempfile.mkdtemp() try: fd, conn_info_file = tempfile.mkstemp(dir=conn_info_dir) os.close(fd) os.unlink(conn_info_file) env = dict(os.environ) env["_PYFLINK_CONN_INFO_PATH"] = conn_info_file def preexec_func(): # ignore ctrl-c / SIGINT signal.signal(signal.SIGINT, signal.SIG_IGN) # Launch the Java gateway. # We open a pipe to stdin so that the Java gateway can die when the pipe is broken p = Popen(command, stdin=PIPE, preexec_fn=preexec_func, env=env) while not p.poll() and not os.path.isfile(conn_info_file): time.sleep(0.1) if not os.path.isfile(conn_info_file): raise Exception( "Java gateway process exited before sending its port number") with open(conn_info_file, "rb") as info: gateway_port = struct.unpack("!I", info.read(4))[0] callback_port = struct.unpack("!I", info.read(4))[0] finally: shutil.rmtree(conn_info_dir) # Connect to the gateway gateway = JavaGateway(gateway_parameters=GatewayParameters( port=gateway_port, auto_convert=True), callback_server_parameters=CallbackServerParameters( port=callback_port, daemonize=True, daemonize_connections=True)) return gateway