Group: PYTHON WORKSHOP

D I S C U S S I O N
wxFrame 调用一个耗时比较长的线程,并等待线程返回结果的过程中,界面停止响应,怎样解决.

lidongok
09/03/2007 14:21 ... 171hits
    Spiders = []
    t = SpiderThread( "aaa" ,text ) 
    Spiders.append( t ) 
    t = SpiderThread( "bbb",text )
    Spiders.append( t ) 
    t = SpiderThread( "ccc",text )
    Spiders.append( t ) 
    t = SpiderThread( "ddd" ,text)
    Spiders.append( t )
        
    for t in Spiders:
        t.start()  #同时开始4 个线程
            
    while threading.activeCount() > 1:  等待线程结束
        # sets the Event's flag to true -- awaken all waiting vehicles
        for t in Spiders:
            if t.busy == False: #这表明线程执行完毕
                file=open("xml%s-%s.html" %(t.name, t.partno),"w")
                file.write(t.htmlcode)
                file.close()
                print t.name + t.htmlcode
                t.join(0) #结束线程


############## 线程  SpiderThread 代码如下 ################
class SpiderThread( threading.Thread ):
    """Class representing a motor vehicle at an intersection"""

    def __init__( self, threadName,keyword ):
        """Initializes thread"""

        threading.Thread.__init__( self, name = threadName )
        self.keyword = keyword
        self.name = threadName
        self.htmlcode = ""
        self.busy = True
        
    def run( self ):
        self.htmlcode = self.getHtmlCode(self.name, self.keyword )
        self.busy = False

    def getHtmlCode (self, webname, keyword):
        """ 这里面就是调用urllib 去访问网站,并返回HTML源代码的一个函数"""
        ...略
Current topic has 2 replies | Back |
#1 lidongok 09/03/2007 14:20
上述程序可以执行并返回正确结果。就是把几个网站的源文件保存到文件中。但是这是wxPython程序,有界面的,当程序执行到while threading.activeCount() > 1:  这里,就停止响应了,直到所有线程完成工作,界面才开始响应,我想有没有类似VB程序的 Application.DoEvent() 这种方法,让界面可以继续响应用户的控制。
Return To Top | Back