[Tutor] Python threads

tpc at csua.berkeley.edu tpc at csua.berkeley.edu
Wed Jun 9 18:54:51 EDT 2004

hi everybody,
I have created a function called determinePlaytimeInSeconds that, given a
tuple of, say, 20 tuples consisting of filename and URI:

(('filename01', 'http://hostname/audio/filename01.mp3'), ('filename02',
'http://hostname/audio/filename02.mp3'), ('filename03',
'http://hostname/video/filename03.wmv'), etc...)

passes each tuple to a threaded class called FileReader.  However,
for some unknown reason, when I run the script I sometimes (though not
always) get a strange error message:

AttributeError: 'FileReader' object has no attribute 'playtimeInSeconds'
AttributeError: 'FileReader' object has no attribute 'fileType'

I somehow think this error has something to do with the thread not
sleeping; however, when I don't comment out the activeCount and sleep
code, the script just hangs.  I used the example code at:


What am I doing wrong ?

import urllib
from threading import Thread
from threading import activeCount
from time import sleep

def determinePlayTimeInSeconds(results):
        threadList = []
	for result in results:
                fileReader = FileReader(result)
        #while activeCount() > 1:
        for fileReader in threadList:
                (fileType, playtimeInSeconds) = fileReader.getPlaytimeInSeconds()
                print fileType, playtimeInSeconds

class FileReader(Thread):
        def __init__(self, result):
                self.fileName = result[0]
                self.fileURI = result[1]
        def run(self):
		if self.fileURI.endswith("mp3"):
                        self.fileType = "Audio"
			self.playtimeInSeconds = (len(urllib.urlopen(urllib.quote(self.fileURI, ':/')).read()) * 8) / 24000
		elif self.fileURI.endswith("wmv"):
                        self.fileType = "Video"
                        self.playtimeInSeconds = get_asf_time(urllib.urlopen(urllib.quote(self.fileURI, ':/')))
        def getPlaytimeInSeconds(self):
                return (self.fileType, self.playtimeInSeconds)

Traceback (most recent call last):
  File "<pyshell#239>", line 1, in -toplevel-
  File "<pyshell#235>", line 10, in determinePlayTimeInSeconds
    (fileType, playtimeInSeconds) = fileReader.getPlaytimeInSeconds()
  File "<pyshell#233>", line 14, in getPlaytimeInSeconds
    return (self.fileType, self.playtimeInSeconds)
AttributeError: 'FileReader' object has no attribute 'fileType'

More information about the Tutor mailing list