## Copyright (c) 2016-2024 Deephaven Data Labs and Patent Pending#importglobimportitertoolsimportosimportpathlibfromdeephaven_internalimportjvm# TODO(deephaven-core#2592): Generalize start_jvm to work with importlib.resourcesdef_jars_path():returnpathlib.Path(__file__).parent/'jars'def_compiler_directives():return_jars_path()/'dh-compiler-directives.txt'def_default_vmoptions():return_jars_path()/'dh-default.vmoptions'def_jars():return_jars_path().glob('*.jar')DEFAULT_JVM_PROPERTIES={# Default to no init scripts, the built-in py init script will prevent using python stdin'PythonDeephavenSession.initScripts':'',# TODO (deephaven-core#XXXX) this doesn't work yet# # Disable the browser console by default, this is not yet well supported# 'deephaven.console.disable': 'true','LoggerFactory.silenceOnProcessEnvironment':'true','stdout.toLogBuffer':'false','stderr.toLogBuffer':'false','logback.configurationFile':'logback-minimal.xml',}DEFAULT_JVM_ARGS=[# Disable the JVM's signal handling for interactive python consoles - if python will# not be handling signals like ctrl-c (for KeyboardInterrupt), this should be safe to# remove for a small performance gain.'-Xrs',# Disable JIT in certain cases'-XX:+UnlockDiagnosticVMOptions',f"-XX:CompilerDirectivesFile={_compiler_directives()}",f"-XX:VMOptionsFile={_default_vmoptions()}",]# Provide a util func to start the JVM, will use its own defaults if none are offered
[docs]defstart_jvm(jvm_args=None,jvm_properties=None,java_home=None,extra_classpath=[],propfile:str=None,config=None):""" This function uses the default DH property file to embed the Deephaven server and starts a Deephaven Python Script session. """jvm_args=jvm_argsorDEFAULT_JVM_ARGSjvm_properties=jvm_propertiesorDEFAULT_JVM_PROPERTIESjava_home=java_homeoros.environ.get('JAVA_HOME',None)system_properties=dict()ifpropfile:# Build jvm system properties starting with defaults we accept as argssystem_properties.update({'Configuration.rootFile':propfile})# Append user-created args, allowing them to override these valuessystem_properties.update(jvm_properties)# Expand the classpath, so a user can resolve wildcardsexpanded_classpath=list(itertools.chain.from_iterable(glob.iglob(e,recursive=True)foreinextra_classpath))# The full classpath is the classpath needed for our server + the expanded extra classpathjvm_classpath=[str(jar)forjarin_jars()]+expanded_classpath# Append args that, if missing, could cause the jvm to be misconfigured for deephaven and its dependencies# TODO make these less required (i.e. at your own risk, remove them)required_jvm_args=[# Allow access to java.nio.Buffer fields'--add-opens=java.base/java.nio=ALL-UNNAMED',# Allow our hotspot-impl project to access internals'--add-exports=java.management/sun.management=ALL-UNNAMED',# Allow our clock-impl project to access internals'--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED',]ifjvm_argsisNone:jvm_args=required_jvm_argselse:jvm_args.extend(required_jvm_args)jvm.init_jvm(java_home=java_home,# jvm_dll=jvm_dll,jvm_classpath=jvm_classpath,jvm_properties=system_properties,jvm_options=jvm_args,# config_file=config_file,config=config)importjpyjpy.VerboseExceptions.enabled=True