-
Notifications
You must be signed in to change notification settings - Fork 183
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
Launch JVM in new thread #1075
base: master
Are you sure you want to change the base?
Launch JVM in new thread #1075
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1075 +/- ##
==========================================
- Coverage 88.63% 88.60% -0.04%
==========================================
Files 112 112
Lines 10235 10295 +60
Branches 4012 4026 +14
==========================================
+ Hits 9072 9122 +50
- Misses 703 712 +9
- Partials 460 461 +1
Continue to review full report at Codecov.
|
Hi @Thrameos So I am testing this with the following script with import threading
import time
from datetime import datetime
import jpype
from jpype import setupGuiEnvironment, shutdownGuiEnvironment, shutdownJVM, startJVM, JClass
print("JPYPE VERSION", jpype.__version__)
jpype.startJVM()
JFrame = JClass("javax.swing.JFrame")
JLabel = JClass("javax.swing.JLabel")
class CustomGUI:
def __init__(self):
self._frame = self._label = None
def run(self):
print(f"{type(self).__name__}.run: start")
self._frame = frame = JFrame("TIME")
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
self._label = label = JLabel("time: ...")
frame.getContentPane().add(label)
frame.pack()
frame.setVisible(True)
print(f"{type(self).__name__}.run: exit")
def set_label(self, text):
if self._label:
self._label.setText(text)
return True
return False
custom_gui = CustomGUI()
def _update_gui_from_python_thread(gui_instance):
now = datetime.now().isoformat()
print(f"python: updating from thread: {now}")
if not gui_instance.set_label(f"time: {now}"):
print("python: could not update")
t = threading.Timer(1.0, _update_gui_from_python_thread, args=(custom_gui,))
t.start()
print("calling setupGuiEnvironment")
setupGuiEnvironment(custom_gui.run)
# vvvvv the code after setupGuiEnvironment doesn't get called
# here we would want to run python specific things while the app is running...
while True:
print("hello from main")
time.sleep(2.0)
shutdownJVM() Behavior of
Let me know if I can help by providing any debugging information for you. Cheers, |
Just to confirm. When you say You may be able to kludge the behavior by having matplot lib draw a plot before starting the JVM because they should be launching the app handler. But that is just a guess. Assuming it did not help, how do suggest replicating the |
@ap-- I attempted to spawn an AppHelper thread in addition to the Java thread. Perhaps that will resolve the issue. (Still making blind stabs in the dark) |
It'd be really nice, if we have some OSX user test this PR and report back with us. |
Agreed. I tried to get a OSX machine from work to test on, but never had any luck. Thus this one needs to stay open until we get a tester. |
I guess since for a long time nobody volunteered, we shall postpone it after 1.5? |
Hello everyone, sorry, this dropped off my list a while ago. I'm happy to test and provide feedback and I should be able to find time next weekend. In any case please don't let this be a blocker for a new release. Cheers, |
Thanks Andreas, looking forward to the results. |
Just to chime in and say that this solves #1169 on Linux. That issue is about starting the JVM on another thread, and then needing to attach as a daemon, so not a big surprise that it solves the issue. Just wanted to report here though 👍 |
Knowing what I do now the OSX can only be fixed by the jpython method or by delibrately abandoning the main thread so it can serve tge event loop which is a rather poor choice for interactive sessions. But if this solves other issues then I am all for it. Forcing Pythons main thread to be the thread that has to stop the jvm lead to shutdown race conditions. |
This is an attempt to solve the OSX issue in which the JVM main thread and the GUI thread must be separate or the GUI will go into a deadlock. Unfortunately, I have no way to test this particular PR as I don't have access to that architecture. If someone can tell me if this fixes the deadlock issue (or makes it worse), it would be appreciated.