Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

saves process name such that jps can shows a name for Pyterrier processes #527

Merged
merged 3 commits into from
Jan 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions pyterrier/java/_core.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import sys
from pyterrier.java import required_raise, required, before_init, started, mavenresolver, JavaClasses, JavaInitializer, register_config
from typing import Optional
import pyterrier as pt
Expand All @@ -12,6 +13,24 @@
# Java Initialization
# ----------------------------------------------------------

def _get_notebook() -> Optional[str]:
try:
import IPython # type: ignore
except Exception:
return None

# Try to get IPython and return None if not found.
ipython = IPython.get_ipython()
if not ipython:
return None
locals = IPython.get_ipython().user_ns

if "__vsc_ipynb_file__" in locals:
return locals["__vsc_ipynb_file__"]
if "__session__" in locals:
return locals["__session__"]
return None

class CoreJavaInit(JavaInitializer):
def priority(self) -> int:
return -100 # run this initializer before anything else
Expand All @@ -35,6 +54,14 @@ def pre_init(self, jnius_config):
for jar in pt.java.configure['jars']:
jnius_config.add_classpath(jar)

# set the property that makes a process name visible in jps
process_name : str = _get_notebook()
if process_name is None:
process_name = "python[pyterrier]:" + (sys.argv[0] if sys.argv[0] else '<interactive>')
else:
process_name = "jupyter[pyterrier]:" + process_name
jnius_config.add_options("-Dsun.java.command=%s" % process_name)

@required_raise
def post_init(self, jnius):
pt.java.set_log_level(pt.java.configure['log_level'])
Expand Down
Loading