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源代码的一个函数"""
...略