-
Notifications
You must be signed in to change notification settings - Fork 82
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
Auto-dispose/cleanup PyImageJ when Python is shutting down #153
Comments
Shutting down the JVM is a little more complicated than what I thought at first glance. Calling scyjava def shutdown_jvm():
"""Shutdown the JVM.
Shutdown the JVM. Set the jpype.config.destroy_jvm flag to true
to ask JPype to destory the JVM itself. Note that enabling
jpype.config.destroy_jvm can lead to delayed shutdown times while
the JVM is waiting for threads to finish.
"""
jpype.config.destroy_jvm = False
jpype.shutdownJVM() pyimagej import scyjava as sj
import sys
def _signal_handler(signal=None, frame=None):
"""Handle clean exit at shutdown.
Handle clean exit at shutdown and also catch Ctlr + C
keyboard interrupts.
"""
ij.dispose()
sj.shutdown_jvm()
sys.exit(0)
# handle clean exit and catch Ctrl + C KeyboardInterrupt
atexit.register(_signal_handler)
signal.signal(signal.SIGTERM, _signal_handler)
signal.signal(signal.SIGINT, _signal_handler) This however is not actually a clean shutdown. My understanding is by setting the I was going to push this code but found a strange bug I'm not sure how to deal with yet. This code works as intended unless you initialize pyimagej in |
With scyjava 1.4.0, we now have an extensible |
Unfortunately I'm still getting a delay/hang on exit on Windows. Note that I'm using updated Windows 10 in an virtual machine (VirtualBox). After starting pyimagej and calling the GUI with Edit: I'm going to try another box that isn't a virtual machine. I will report back! |
We resolved the delay on exit on Windows by ensuring that we dispose of all windows prior to JVM shutdown. For whatever reason there seems to be a hidden window that |
The latest scyjava 1.4.1 has problems shutting down too eagerly in some scenarios; see this gitter thread for details. |
We ran out of time to investigate this problem any further for the moment. But I added an entry to the Troubleshooting document about it with workarounds (a621108). |
When PyImageJ is running and is suddenly terminated (e.g. interrupting an active GUI session with
Ctrl + C
) can sometimes result in a terminal session waiting forJPype
Java threads to end. This isn't great behavior -- so we should auto cleanup PyImageJ upon termination and exit cleanly.It looks like Python has a couple ways to exit:
We can use atexit() to run the appropriate cleanup routines. This will likely need some new hooks in
scyjava
where the JVM related methods live.Here is a great example using
atexit()
in addition to catchingSIGINT
generated from theCtrl + C
keyboard interrupt.And here's the relevant
JPype
docs section regarding handling JVM shutdown.The text was updated successfully, but these errors were encountered: