Do thread die?
Maurice LING
mauriceling at acm.org
Sat Sep 17 09:19:06 EDT 2005
>
>> My current implementation may be ugly. I have a class
>>
>> class myThread(threading.Thread):
>> def __init__(self, func):
>> self.func = func
>> threading.Thread.__init__(self)
>> def run(self):
>> print '%s function running' % self.func
>> self.func()
>>
>> which is used in
>>
>> class myClass: #using myThread
>> def __init__(self): pass
>> def aFunc(self): pass
>> def bFunc(self): pass
>> def runAll(self):
>> myThread(self.aFunc).start()
>> myThread(self.bFunc).start()
>
>
>
> There is a school of thought that says that a derived object should
> never be altered such that it cannot be used as its parent could. I
> happen to agree largely with this. Now, your MyThread implementation is
> _reducing_ the functionality of its ancestor Thread object such that you
> could not use a myThread in place of a Thread. I believe that you should
> not be subclassing Thread to do this. Your myClass doesn't need it
> anyway. Look at this modified myClass:
>
> class myClass2:
> def aFunc(self): pass
> def bFunc(self): pass
> def runAll(self):
> threading.Thread(target=self.aFunc).start()
> threading.Thread(target=self.bFunc).start()
>
Thanks everyone. Furthering that, is the following legal?
class myClass3:
def aFunc(self, a): pass
def bFunc(self, b): pass
def runAll(self, a, b):
threading.Thread(target=self.aFunc, args = (a)).start()
threading.Thread(target=self.bFunc, args = (b)).start()
I do have another dumb question which is OT here. Say aFunc method
instantiates a SOAP server that serves forever, will it prevent bFunc
from running as a separate thread?
For example,
class myClass4:
def repeat(self, s): return s+s
def aFunc(self, a):
import SOAPpy
serv = SOAPpy.SOAPServer((a[0], a[1]))
serv.registerFunction(repeat)
serv.serve_forever()
def bFunc(self, b): pass
def runAll(self, a, b):
threading.Thread(target=self.aFunc, args = (a)).start()
threading.Thread(target=self.bFunc, args = (b)).start()
if __name__=='__main__': myClass4().runAll(['localhost', 8000], 'hi')
Will the 2nd thread (bFunc) ever run since the 1st thread is running
forever? Intuitively, I think that both threads will run but I just want
to be doubly sure, because some of my program logic depends on the 2nd
thread running while the 1st thread acts as a SOAP server or something.
Thanks and Cheers
Maurice
More information about the Python-list
mailing list