no parallel threading?

Tobias Unruh Tobias_Unruh at FRM2.TU-Muenchen.DE
Wed Sep 5 17:18:20 CEST 2001


Hi,

I tried to execute two threads in parallel (linux 2.4.9, python 2.0):

import time
import threading

def test_thread_func1():
    print "Hallo1"
    time.sleep(3.0)
    print "Hallo1"

def test_thread_func2():
    print "Hallo2"
    time.sleep(3.0)
    print "Hallo2"

printlock = threading.Lock()

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()

print "number of active threads: %d" % threading.activeCount()

while test_thread1.isAlive()==1:
    continue

print "number of active threads: %d" % threading.activeCount()


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 this case,
however, the number of active threads should be 1 in any case. But these
values are ok. Is there something wrong with the output?
sys.stdout.flush() does not help.

Any suggestions?

Tobias

--
Tobias Unruh
TU Muenchen, FRM-II
85747 Garching, Germany





More information about the Python-list mailing list