Connecting Tech Pros Worldwide Forums | Help | Site Map

mutex? protecting global data/critical section

Brian Alexander
Guest
 
Posts: n/a
#1: Jul 18 '05
Hello;

I am trying to protect some global data, which may, in the future, be
accessed by threads. I'm not sure how to implement a locking mechanism
in python. Here is the idea I'm trying to express:

class resourceManager(object):
def __init__(self):
self.__resources = 100

def getResource(self):
BEGIN CRITICAL SECTION HERE
if self.__resources > 0:
self.__resources -= 1
* other related state information adjusted, too *
else:
emergencyCode()
END CRITICAL SECTION HERE

How is this accomplished? Does anyone know of a simple example?

Many thanks.

Brian.


Dave Brueck
Guest
 
Posts: n/a
#2: Jul 18 '05

re: mutex? protecting global data/critical section


On Friday 19 September 2003 01:10 pm, Brian Alexander wrote:[color=blue]
> Hello;
>
> I am trying to protect some global data, which may, in the future, be
> accessed by threads. I'm not sure how to implement a locking mechanism
> in python. Here is the idea I'm trying to express:
>
> class resourceManager(object):
> def __init__(self):
> self.__resources = 100
>
> def getResource(self):
> BEGIN CRITICAL SECTION HERE
> if self.__resources > 0:
> self.__resources -= 1
> * other related state information adjusted, too *
> else:
> emergencyCode()
> END CRITICAL SECTION HERE
>
> How is this accomplished? Does anyone know of a simple example?[/color]

The basic idiom is to create a lock object somewhere:
lock = threading.Lock()
....

and then your critical section looks like:

lock.acquire()
try:
dostuff
finally:
lock.release()

-Dave

Skip Montanaro
Guest
 
Posts: n/a
#3: Jul 18 '05

re: mutex? protecting global data/critical section



Brian> I am trying to protect some global data, which may, in the
Brian> future, be accessed by threads. I'm not sure how to implement a
Brian> locking mechanism in python.

There's generally no need to roll your own locking. Start simple (Queue
module) and get more sophisticated (threading module's Lock, RLock,
Semaphore, Condition or Event classes). Almost all the time, a Queue object
will do the trick:

class resourceManager(object):
def __init__(self):
self.__queue = Queue.Queue()
for i in range(100)
self.__queue.put(None)

def getResource(self):
self.__queue.get()
do my thing
self.__queue.put(None)

In the above case, you're not actually sharing anything, just using
self.__queue to limit the number of simultaneous threads. More likely, you
will either have a certain number of objects which are limited by the
performance of your system (say, database connections) or some piece of
global state which you need to control access to (e.g., some kind of
blackboard object). Either one might look something like this:

class resourceManager(object):
def __init__(self, list_of_stuff):
self.__queue = Queue.Queue()
for item in list_of_stuff
self.__queue.put(item)

def apply(self, func):
item = self.__queue.get()
result = func(item)
self.__queue.put(item)
return result

manager = resourceManager([blackboard])
...
manager.apply(write_on_blackboard)


dbmanager = resourceManager([conn1, conn2, conn3])
...
dbmanager.apply(execute_sql)

Of course, you will probably want a bit more general apply() method so you
can pass other arguments to the function.

Skip

Closed Thread


Similar Python bytes