no parallel threading?

Fredrik Lundh fredrik at pythonware.com
Wed Sep 5 17:05:48 EDT 2001


Tobias Unruh wrote:

> test_thread1 = threading.Thread(None,test_thread_func1())
> test_thread2 = threading.Thread(None,test_thread_func2())
>
> print "number of active threads: %d" % threading.activeCount()
>
> test_thread1.start()
> test_thread2.start()
>
> The output of the program is:
>
> Hallo1
> Hallo1
> Hallo2
> Hallo2
> number of active threads: 1
> number of active threads: 3
> number of active threads: 1
>
> Why this result? From the first four lines I concluded that the threads
> are executed one after the other (which is not intended).

in fact, they even finished *before* you started them (before
the first activeCount print statement).

your example probably works better if you let the thread module
run your functions for you, instead of passing their return values
to the Thread constructor.

like this:

    test_thread1 = threading.Thread(None,test_thread_func1)
    test_thread2 = threading.Thread(None,test_thread_func2)

this gives:

    number of active threads: 1
    Hallo1
    Hallo2
    number of active threads: 3
    Hallo1
    number of active threads: 2
    Hallo2

which is probably closer to what you expected.

</F>





More information about the Python-list mailing list