python - "QThread: Destroyed while thread is still running" when run from the Windows cmd or IDLE but not from PyCharm? -


this simplified version of program implementing pyqt multi-threading qobject.movetothread. basically, query webpage on separate thread , extract hmtl content.

i problem running code idle or windows command line hangs python. windows cmd shows "qthread: destroyed while thread still running". however, if run pycharm, works fine.

you can .ui file here

any ideas?

import requests import sys pyqt4 import qtgui, uic pyqt4.qtcore import qobject, pyqtslot, pyqtsignal, qthread   qtcreatorfile = "window.ui" ui_mainwindow, qtbaseclass = uic.loaduitype(qtcreatorfile)   class httpclient(qobject):      finished =  pyqtsignal(str)      def __init__(self):         qobject.__init__(self)      @pyqtslot()     def retrieve_page(self, url):         response = requests.get(url)         self.finished.emit(response.text)   class htmlgetter(qtgui.qmainwindow, ui_mainwindow):      def __init__(self):         qtgui.qmainwindow.__init__(self)         ui_mainwindow.__init__(self)         self.setupui(self)                self.go_button.clicked.connect(self.query_page)      def query_page(self):         http_client = httpclient()         temp_thread = qthread()         http_client.movetothread(temp_thread)          temp_thread.started.connect(         lambda: http_client.retrieve_page("http://www.google.com/"))         http_client.finished.connect(self.show_html)          # terminating thread gracefully.         http_client.finished.connect(temp_thread.quit)         http_client.finished.connect(http_client.deletelater)         temp_thread.finished.connect(temp_thread.deletelater)          temp_thread.start()      def show_html(self, html_text):         print(html_text)   def main():     app = qtgui.qapplication(sys.argv)     window = htmlgetter()     window.show()     sys.exit(app.exec_())   if __name__ == '__main__':     main() 

i figured out:

both http_client , temp_thread have attributes or htmlgetter class. think it's because otherwise python discards them when exiting function. working code:

import requests import sys pyqt4 import qtgui, uic pyqt4.qtcore import qobject, pyqtslot, pyqtsignal, qthread   qtcreatorfile = "window.ui" ui_mainwindow, qtbaseclass = uic.loaduitype(qtcreatorfile)   class httpclient(qobject):      finished =  pyqtsignal()     send_text = pyqtsignal(str)      def __init__(self):         qobject.__init__(self)      @pyqtslot()     def retrieve_page(self, url):         response = requests.get(url)         self.send_text.emit(response.text)         self.finished.emit()   class htmlgetter(qtgui.qmainwindow, ui_mainwindow):      def __init__(self):         qtgui.qmainwindow.__init__(self)         ui_mainwindow.__init__(self)         self.setupui(self)                self.go_button.clicked.connect(self.query_page)      def query_page(self):         self.http_client = httpclient()         self.temp_thread = qthread()         self.http_client.movetothread(self.temp_thread)          self.temp_thread.started.connect(         lambda: self.http_client.retrieve_page("http://www.google.com/"))         self.http_client.send_text.connect(self.show_html)          # terminating thread gracefully.         self.http_client.finished.connect(self.temp_thread.quit)         self.http_client.finished.connect(self.http_client.deletelater)         self.temp_thread.finished.connect(self.temp_thread.deletelater)          self.temp_thread.start()      def show_html(self, html_text):         print(html_text)   def main():     app = qtgui.qapplication(sys.argv)     window = htmlgetter()     window.show()     sys.exit(app.exec_())   if __name__ == '__main__':     main() 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -