passing threading.Thread() and function object

Jeremy Jones zanesdad at bellsouth.net
Wed Oct 13 20:30:22 CEST 2004


Christopher J. Bottaro wrote:

>Hello,
>
>I'm new to Python programming, so please excuse me.
>
>thread = threading.Thread(self.somefunc())
>thread.start()
>print "Thread started"
>
>def somefunc(self):
>        while (1)
>                print "In thread"
>
>
>"Thread started" never gets printed, but "In thread" gets printed
>repeatedly.  What is going on?  It seems like thread.start() is blocking
>and effectively running self.somefunc() as a normal function.
>
>Thanks for the help.
>
>  
>
Your code is evaluating self.somefunc(), which is an infinite loop.  The 
thread is actually never starting.  You may want to do something more 
like this:



<untested code>

import threading

class DoSomething(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
    def run(self):
        while 1:
            print "In thread"

if __name__ == "__main__":
    ds = DoSomething()
    ds.start()
    print "Thread started"

</untested code>



Jeremy Jones



More information about the Python-list mailing list