2013. 9. 6.

Sending Data Between Threads by using QThread, at pyhon (QT version)

##  Do Asynchronously and return result to main Thread
##
##  step 1:  Define thread class
##  step 2:  Call 'emit' to send notify+data to Main thread  
##  step 3:  Connect event and method
##  step 4:  Implement method 


from time import sleep
from PyQt4 import QtCore

class ParentThread(QtCore.QThread):
    def __init__(self):
        QtCore.QThread.__init__(self)
        self.exiting = False

    def __del__(self):
        self.exiting = True     # used for exit in whilie loop.
        self.wait()



class ChildThread(ParentThread):

    def __init__(self):
        ParentThread.__init__(self)
 
    def run(self): # this method does not use loop code, it run once.
        
        # get server version data
        conn = httplib.HTTPConnection("webserveraddress")
        conn.request("GET", "/data.htm")
        connResponse = conn.getresponse()  # reason, status
        resData = connResponse.read()

        #send message + event
        # resdata is passed as parameter of onComplete
        self.emit( QtCore.SIGNAL('onComplete(QString)') , resData)  



class MainClass:

    def __init__(self, parent=None):    
        self.childThread= ChildThread()

       #connect event
        self.connect( self.childThread, QtCore.SIGNAL('onComplete(QString)'), self.onComplete)
        
        #start Thread
        self.versionThread.start()  

   def onComplete(self, resData):
        print resData


        


Enhanced by Zemanta

댓글 없음:

댓글 쓰기