473,399 Members | 3,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Threading Oddity?

I noticed a strange yet easily fixed problem in Python's thread
module.

Consider the following code:

#-------------------------------

import thread

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

def thread1():
while data_mutex.locked():
data_mutex.aquire()
print 'thread1:',data
if data_mutex.locked():
data_mutex.release()

def thread2():
while data_mutex.locked():
data_mutex.aquire()
print 'thread2:',data
if data_mutex.locked():
data_mutex.release()

thread.start_new_thread(thread1,())
thread.start_new_thread(thread2,())

#-------------------------------

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. The Python Library Reference
mentions that "this method acquires the lock unconditionally, if
necessary waiting until it is released by another thread (only one
thread at a time can acquire a lock -- that's their reason for
existence), and returns None." You'd think one wouldn't have to check
if the lock is locked, especially when acquire is supposed to wait for
the lock to be freed. At first, I thought this problem had to do with
aquire()'s waitflag, but the problem exists regardless of waitflag's
value.

Does anyone know why this problem exists, and if my code works around
it? It would seem that locked() and acquire() would not necessarily be
run uninterrupted, and so mutual exclusion could not be guaranteed.
Jul 18 '05 #1
8 3417
use this instead and you're fine:

import threading
data_mutex = threading.Lock()

using the thread module directly is depreciated, and the threading.Lock()
object works as you expect without having to check if it's locked before
acquiring it.

Kevin


"Kris Caselden" <go****@hanger.snowbird.net> wrote in message
news:ab**************************@posting.google.c om...
I noticed a strange yet easily fixed problem in Python's thread
module.

Consider the following code:

#-------------------------------

import thread

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

def thread1():
while data_mutex.locked():
data_mutex.aquire()
print 'thread1:',data
if data_mutex.locked():
data_mutex.release()

def thread2():
while data_mutex.locked():
data_mutex.aquire()
print 'thread2:',data
if data_mutex.locked():
data_mutex.release()

thread.start_new_thread(thread1,())
thread.start_new_thread(thread2,())

#-------------------------------

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. The Python Library Reference
mentions that "this method acquires the lock unconditionally, if
necessary waiting until it is released by another thread (only one
thread at a time can acquire a lock -- that's their reason for
existence), and returns None." You'd think one wouldn't have to check
if the lock is locked, especially when acquire is supposed to wait for
the lock to be freed. At first, I thought this problem had to do with
aquire()'s waitflag, but the problem exists regardless of waitflag's
value.

Does anyone know why this problem exists, and if my code works around
it? It would seem that locked() and acquire() would not necessarily be
run uninterrupted, and so mutual exclusion could not be guaranteed.

Jul 18 '05 #2
use this instead and you're fine:

import threading
data_mutex = threading.Lock()

using the thread module directly is depreciated, and the threading.Lock()
object works as you expect without having to check if it's locked before
acquiring it.

Kevin


"Kris Caselden" <go****@hanger.snowbird.net> wrote in message
news:ab**************************@posting.google.c om...
I noticed a strange yet easily fixed problem in Python's thread
module.

Consider the following code:

#-------------------------------

import thread

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

def thread1():
while data_mutex.locked():
data_mutex.aquire()
print 'thread1:',data
if data_mutex.locked():
data_mutex.release()

def thread2():
while data_mutex.locked():
data_mutex.aquire()
print 'thread2:',data
if data_mutex.locked():
data_mutex.release()

thread.start_new_thread(thread1,())
thread.start_new_thread(thread2,())

#-------------------------------

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. The Python Library Reference
mentions that "this method acquires the lock unconditionally, if
necessary waiting until it is released by another thread (only one
thread at a time can acquire a lock -- that's their reason for
existence), and returns None." You'd think one wouldn't have to check
if the lock is locked, especially when acquire is supposed to wait for
the lock to be freed. At first, I thought this problem had to do with
aquire()'s waitflag, but the problem exists regardless of waitflag's
value.

Does anyone know why this problem exists, and if my code works around
it? It would seem that locked() and acquire() would not necessarily be
run uninterrupted, and so mutual exclusion could not be guaranteed.

Jul 18 '05 #3
On 22 Aug 2003 21:33:18 -0700, rumours say that
go****@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.
Jul 18 '05 #4
On 22 Aug 2003 21:33:18 -0700, rumours say that
go****@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.
Jul 18 '05 #5
Christos "TZOTZIOY" Georgiou <tz**@sil-tec.gr> wrote in message news:<jr********************************@4ax.com>. ..
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".


You lost me for a second, until I realized that "aquire" and "acquire"
are two different words ;) Sigh. However, my code does indeed run,
without error, which begs the question; how is this working?

Anyways, thanks for the fix.
Jul 18 '05 #6
Christos "TZOTZIOY" Georgiou <tz**@sil-tec.gr> wrote in message news:<jr********************************@4ax.com>. ..
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".


You lost me for a second, until I realized that "aquire" and "acquire"
are two different words ;) Sigh. However, my code does indeed run,
without error, which begs the question; how is this working?

Anyways, thanks for the fix.
Jul 18 '05 #7
On 23 Aug 2003 14:26:28 -0700, rumours say that
go****@hanger.snowbird.net (Kris Caselden) might have written:
However, my code does indeed run,
without error, which begs the question; how is this working?


Kris, I believe your code runs without error in your hardware, and I
also know my code runs without error on my hardware. What your post
initially implied is that /my/ code wouldn't run on /your/ hardware
unless you wrapped the acquire calls in "while loop checks for mutex
lock". Did you run my code successfully? If yes, we can skip that
question as implicitly answered, if not, come back with more info.

In your latest question, "how is this working?", do you mean how mutex
locks work? The answer to this is that there are operating system calls
whose purpose is to implement such locks, and Python builds on that. If
I did not understand your question, please let me know.
--
TZOTZIOY, I speak England very best,
Microsoft Security Alert: the Matrix began as open source.
Jul 18 '05 #8
On Fri, Aug 22, 2003 at 09:33:18PM -0700, Kris Caselden wrote:
I noticed a strange yet easily fixed problem in Python's thread
module.

Consider the following code:

#-------------------------------

import thread

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

def thread1():
while data_mutex.locked():
data_mutex.aquire()
print 'thread1:',data
if data_mutex.locked():
data_mutex.release()

def thread2():
while data_mutex.locked():
data_mutex.aquire()
print 'thread2:',data
if data_mutex.locked():
data_mutex.release()

thread.start_new_thread(thread1,())
thread.start_new_thread(thread2,())


This code didn't run for me (no lines were printed) because the program
exits when the main thread is done. I added
import time
time.sleep(1)
after the start_new_thread calls as a remedy. Then, it printed two
lines and waited about a second before I got the prompt back.

I tried removing the 'while's and dedenting the lines
"data_mutex.aquire". Then I got an attribute error!

But with the "while", there's no error. Why not? Because the mutex was
never locked! If the mutex is not locked, the body of the while statement
is not executed. On the other hand, if the statement inside the loop
was executed and did successfully claim the mutex, the 'while' condition
would evaluate to 'true' the next time through, the statement to claim
the lock would execute again, and block forever. (but this isn't likely
to happen on any particular run, because it would require that the mutex
be released at just the right moment.. properly testing threaded code is
hard)

I think this comes down to being a typo (as pointed out by another
poster, acquire vs aquire) and the addition of the 'while' loop just
adds confusion and is a completely wrong thing to do.

Jeff

Jul 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Michael Foord | last post by:
Here's a little oddity with 'print' being a reserved word... >>> class thing: pass >>> something = thing() >>> something.print = 3 SyntaxError: invalid syntax >>> print something.__dict__...
5
by: jmdocherty | last post by:
All, I've been trying to set up a CSS layout and all seems well in Firefox but in Safari it just seems to be plain weird! I hope someone can help tell me whether this is a problem with my code...
43
by: michael.f.ellis | last post by:
The following script puzzles me. It creates two nested lists that compare identically. After identical element assignments, the lists are different. In one case, a single element is replaced. In...
6
by: Rex the Strange | last post by:
Hello all, Traditionally I'm a Delphi and VB6 programmer and have recently started working with VB.NET (which, I might add, I love, but that's beside the point). Anyway, I was trying to make a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.