Threading Oddity?

Christos TZOTZIOY Georgiou tzot at sil-tec.gr
Sat Aug 23 09:54:24 EDT 2003


On 22 Aug 2003 21:33:18 -0700, rumours say that
google at hanger.snowbird.net (Kris Caselden) might have written:

[code snip]
>def thread1():
>    while data_mutex.locked():
>        data_mutex.aquire()
>    print 'thread1:',data
>    if data_mutex.locked():
>        data_mutex.release()
[code snip]

Kris, please copy-paste *real* code.  This is not code you ran, this was
written for the sake of the post.  thread.lock object has no attribute
"aquire".

>This code runs as expected. However, take away the while loop checks
>for mutex lock and Python is unable to properly acquire the lock,
>complaining of an unhandled exception.

I never had any problems with thread.lock.acquire, and I use it a lot.
Please come back with the code that fails for you, and specify the
environment (OS, python version, whether you built it from sources, if
you applied any changes to the source etc).

The following program runs fine for me on Linux, Irix and Windows 2k:

import thread, time

data='this is data'
data_mutex = thread.allocate_lock()

def thread1():
    data_mutex.acquire()
    print 'thread1:',data
    data_mutex.release()

def thread2():
    data_mutex.acquire()
    print 'thread2:',data
    data_mutex.release()

thread.start_new_thread(thread1,())
thread.start_new_thread(thread2,())
time.sleep(1)

I inserted the time.sleep(1) to allow for the threads to run before the
program ends (and thus the threads end prematurely).  threading would
allow you to .join the threads instead.
-- 
TZOTZIOY, I speak England very best,
Microsoft Security Alert: the Matrix began as open source.




More information about the Python-list mailing list