python - Background thread with QThread in PyQt -


i have program interfaces radio using via gui wrote in pyqt. 1 of main functions of radio transmit data, continuously, have loop writes, causes gui hang. since have never dealt threading, tried rid of these hangs using qcoreapplication.processevents(). radio needs sleep between transmissions, though, gui still hangs based on how long these sleeps last.

is there simple way fix using qthread? have looked tutorials on how implement multithreading pyqt, of them deal setting servers , more advanced need them be. don't need thread update while running, need start it, have transmit in background, , stop it.

i created little example shows 3 different , simple ways of dealing threads. hope find right approach problem.

import sys import time  pyqt5.qtcore import (qcoreapplication, qobject, qrunnable, qthread,                           qthreadpool, pyqtsignal)   # subclassing qthread # http://qt-project.org/doc/latest/qthread.html class athread(qthread):      def run(self):         count = 0         while count < 5:             time.sleep(1)             print("a increasing")             count += 1  # subclassing qobject , using movetothread # http://blog.qt.digia.com/blog/2007/07/05/qthreads-no-longer-abstract class someobject(qobject):      finished = pyqtsignal()      def long_running(self):         count = 0         while count < 5:             time.sleep(1)             print("b increasing")             count += 1         self.finished.emit()  # using qrunnable # http://qt-project.org/doc/latest/qthreadpool.html # note qrunnable isn't subclass of qobject , therefore # not provide signals , slots. class runnable(qrunnable):      def run(self):         count = 0         app = qcoreapplication.instance()         while count < 5:             print("c increasing")             time.sleep(1)             count += 1         app.quit()   def using_q_thread():     app = qcoreapplication([])     thread = athread()     thread.finished.connect(app.exit)     thread.start()     sys.exit(app.exec_())  def using_move_to_thread():     app = qcoreapplication([])     objthread = qthread()     obj = someobject()     obj.movetothread(objthread)     obj.finished.connect(objthread.quit)     objthread.started.connect(obj.long_running)     objthread.finished.connect(app.exit)     objthread.start()     sys.exit(app.exec_())  def using_q_runnable():     app = qcoreapplication([])     runnable = runnable()     qthreadpool.globalinstance().start(runnable)     sys.exit(app.exec_())  if __name__ == "__main__":     #using_q_thread()     #using_move_to_thread()     using_q_runnable() 

Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -