mutex? protecting global data/critical section

Discussion in 'Python' started by Dave Brueck, Sep 18, 2003.

  1. Dave Brueck

    Dave Brueck Guest

    On Friday 19 September 2003 01:10 pm, Brian Alexander wrote:
    > 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?


    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
     
    Dave Brueck, Sep 18, 2003
    #1
    1. Advertising

  2. 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.
     
    Brian Alexander, Sep 19, 2003
    #2
    1. Advertising

  3. 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
     
    Skip Montanaro, Sep 19, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bob Barrows
    Replies:
    0
    Views:
    400
    Bob Barrows
    Feb 27, 2004
  2. NaeiKinDus
    Replies:
    1
    Views:
    577
    Jack Klein
    Apr 14, 2007
  3. NaeiKinDus
    Replies:
    3
    Views:
    610
    James Kanze
    Apr 15, 2007
  4. aeromarine
    Replies:
    15
    Views:
    1,490
    Martin
    Feb 18, 2008
  5. sven
    Replies:
    2
    Views:
    1,947
    Roy Smith
    Dec 4, 2009
Loading...

Share This Page