Noob can't make threads work

Grant Edwards grante at visi.com
Tue Mar 1 14:10:37 EST 2005


On 2005-03-01, Steve Horsley <shoot at the.moon> wrote:
> I am trying to start two threads to do some time consuming work. This is my
> first stab at threading, and it isn't working as I expect. Instead of the 
> threads starting when I call start(), they seem to run the target code as
> part of the constructor call.
>
> Here is my test code...
>
> #!/usr/bin/python
>
> import time
> import threading
>
> def fiddle():
>     for n in range(3):
>         print n
>         time.sleep(1)
>
> print 'Creating threads...'
> t1 = threading.Thread(target=fiddle())
> t2 = threading.Thread(target=fiddle())

t1 = threading.Thread(target=fiddle)
t2 = threading.Thread(target=fiddle)


> print 'Starting threads...'
> t1.start()
> t2.start()
>
>
> I was expecting output like this:
>
> Creating threads...
> Starting threads...
> 0
> 0
> 1
> 1
> 2
> 2
>
> but I get this instead:
>
> Creating threads...
> 0
> 1
> 2
> 0
> 1
> 2
> Starting threads...
>
> This is in python 2.3 on Linux and also python 2.4 on XP.
> Either threading in Python is badly broken, or I'm missing something
> fundamental. I know which is most likely, but I can't figure it out.
>
> Could someone point me in the right direction, plese?
>
> TIA
> Steve


-- 
Grant Edwards                   grante             Yow!  We just joined the
                                  at               civil hair patrol!
                               visi.com            



More information about the Python-list mailing list