Mine værktøjer
Du er her: Forside diverse filer til download mm. threadTest.py
Handlinger tilknyttet webside

threadTest.py

af adminSidst opdateret 23/06 2007 16:14

Viser threads i python

Klik her for at hente fil

Størrelse: 2.0 kB - Filtype text/python-source

Indhold af fil-indholdsobjekt

#!/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()
Relateret indhold
« august 2018 »
søn man tir ons tor fre lør
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Chat Rooms
Dyrk motion

Trim ballerup

 

Powered by Plone, the Open Source Content Management System