Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > RE: Threading - Why Not Lock Objects Rather than lock theinterpreter

Thread Tools

RE: Threading - Why Not Lock Objects Rather than lock theinterpreter

Robert Brewer
Posts: n/a
Fuzzyman wrote:
> Looking at threading code it seems that the Global Interpreter Lock is
> a bit of a 'brute force' way of preventing two threads attempting to
> access sensitive objects simultaneously....
> Why not have a new type of object with a 'lock' facility... if an
> object is locked then any thread trying to access the object other
> than the one obtaining the lock is blocked until the lock is
> released..... rather than blocking *all* threads until one thread has
> finished with the object.....
> It could be implemented as a new attribute of existing objects... or
> new types of objects.....

You mean like an RLock?

acquire( [blocking = 1])
Acquire a lock, blocking or non-blocking.
When invoked without arguments: if this thread already
owns the lock, increment the recursion level by one,
and return immediately. Otherwise, if another thread
owns the lock, block until the lock is unlocked. Once
the lock is unlocked (not owned by any thread), then
grab ownership, set the recursion level to one, and
return. If more than one thread is blocked waiting until
the lock is unlocked, only one at a time will be able
to grab ownership of the lock.

I use this for dictionaries, where I don't want it modified while
another thread is iterating over it:

import threading
class UnitCollection(dict):
_mutex = None

def __init__(self):
self._mutex = threading.RLock()

def acquire(self):

def release(self):

Robert Brewer
Amor Ministries Removed)

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
why is most microsoft courses not practical rather than multipul c discodave MCDST 4 10-25-2007 01:02 PM
Why use object pointers rather than objects? C++ 6 10-16-2007 08:49 AM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Threading - Why Not Lock Objects Rather than lock the interpreter Fuzzyman Python 3 12-05-2003 10:43 PM
File test operator (-r) returns 'not exist' rather than 'not readable' Justin Perl Misc 4 10-30-2003 10:55 PM