threadTest.py
by
admin
—
last modified
2007-06-23 16:14
Viser threads i python
Size 2.0 kB - File type text/python-sourceFile contents
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# se http://www.eksperten.dk/spm/782383
# program som downloader sider med aktiekurser fra yahoo
import time
import threading
import urllib
class testit( threading.Thread ):
def __init__ ( self, url ):
threading.Thread.__init__( self )
self.url = url
self.done = False
def run( self ):
self.status = True
try:
site = urllib.urlopen( self.url )
self.data = site.read()
except:
self.status = False
self.done = True
if __name__ == "__main__":
print time.ctime()
aktieList = ["1587", "BIFB", "EDB", "HARTB",
"NVO", "SAS", "TKDV", "21038", "BIOMAR",
"DFDS", "GEN", "KEOPS", "NZYM", "SCHO", "TORM",
"3248", "BIOS", "CODAN", "DISK", "GES", "LUN",
"ORION", "SIM", "TPSL", "AOJP", "BOB", "COLO-B",
"MAERSKB", "PHARMX", "SJGR", "VWS",
"AURIB", "DANIO", "DSVENA", "GPVB",
"NDA", "PAALB", "SOEN", "WDH", "BAVA", "CAPI",
"DCO", "DSV", "GR4SEC", "NEUR", "RTX",
]
report = { True:"er indlæst", False:"er ikke indlæst", }
urlList = [ "http://finance.yahoo.com/q?s=%s.CO&d=t" % ( x ) for x in aktieList ]
ThreadList = []
for url in urlList:
siteThread = testit( url ) # opret Thread objekt
ThreadList.append( siteThread ) # sæt Thread object ind i list
siteThread.start() # start thread objekt parallelt.
while ThreadList:
siteThread = ThreadList.pop(0)
if siteThread.done:
print siteThread.url, report[ siteThread.status ]
if siteThread.status: # hvis indlæst skriv lige de første 100 tegn fra siden ud.
print siteThread.data[ :100 ], "..."
else:
print ".",
time.sleep( 0.1 )
ThreadList.append( siteThread )
print time.ctime()
Click here to get the file