Skip to content

Instantly share code, notes, and snippets.

@altendky
Last active April 19, 2022 21:11
Show Gist options
  • Save altendky/faf48446c335418dac8793635d119750 to your computer and use it in GitHub Desktop.
Save altendky/faf48446c335418dac8793635d119750 to your computer and use it in GitHub Desktop.
import qt5reactor
import sys
import time
import twisted
from PyQt5 import QtCore
def thread():
print('threading')
time.sleep(1)
print('done threading')
def quit():
print('quitting')
# TODO: _stopThreadPool() should not be necessary but otherwise
# it hangs on quit. Killing results in:
#
# Exception ignored in: <module 'threading' from '/usr/lib/python3.5/threading.py'>
# Traceback (most recent call last):
# File "/usr/lib/python3.5/threading.py", line 1288, in _shutdown
# t.join()
# File "/usr/lib/python3.5/threading.py", line 1054, in join
# self._wait_for_tstate_lock()
# File "/usr/lib/python3.5/threading.py", line 1070, in _wait_for_tstate_lock
# elif lock.acquire(block, timeout):
# KeyboardInterrupt
twisted.internet.reactor._stopThreadPool()
app.quit()
print('already quit')
app = QtCore.QCoreApplication(sys.argv)
qt5reactor.install()
# TODO: getThreadPool().start() should not be required but otherwise
# it hangs without running thread()
twisted.internet.reactor.getThreadPool().start()
d = twisted.internet.threads.deferToThread(thread)
d.addCallback(lambda _: quit())
sys.exit(app.exec())
import qt5reactor
import sys
import time
import twisted
from PyQt5 import QtCore
def thread():
print('threading')
time.sleep(1)
print('done threading')
def quit():
print('quitting')
app.quit()
print('already quit')
app = QtCore.QCoreApplication(sys.argv)
qt5reactor.install()
d = twisted.internet.threads.deferToThread(thread)
d.addCallback(lambda _: quit())
import twisted.internet
sys.exit(twisted.internet.reactor.run())
import sys
import time
import twisted.internet
import twisted.internet.reactor
import twisted.internet.threads
def thread():
print('threading')
time.sleep(1)
print('done threading')
def quit():
print('quitting')
twisted.internet.reactor.stop()
print('already quit')
d = twisted.internet.threads.deferToThread(thread)
d.addCallback(lambda _: quit())
sys.exit(twisted.internet.reactor.run())
@brookbot
Copy link

Replacing
sys.exit(app.exec())
with
sys.exit(twisted.internet.reactor.run())
works for me, and I can kill the application with CRTL-C, however my "quit" menu item no longer works - even when I define quit and link it to my thread. It seems to get stuck on a thread.join() call from my Qt window.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment