473,322 Members | 1,699 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,322 software developers and data experts.

mutex? protecting global data/critical section

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.

Jul 18 '05 #1
2 5395
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

Jul 18 '05 #2

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

Jul 18 '05 #3

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

Similar topics

193
by: Michael B. | last post by:
I was just thinking about this, specifically wondering if there's any features that the C specification currently lacks, and which may be included in some future standardization. Of course, I...
2
by: Martin Maat | last post by:
Hi. I want to use the same mutex in different classes (web pages in an ASP.NET application). In global.asax.cs, the class that starts up first, I create a Mutex like this: static private...
2
by: Lowell | last post by:
I want a (global) mutex which prevents a section of my code from being run by more than one process (or user) at a time. I have something that works, EXCEPT when there is an exception of some kind. ...
3
by: dercon | last post by:
I'm developing a multi-threaded application which contains a global collection. The main thread adds objects to the global collection while the worker threads (between 10-20) read (for each......
16
by: mfdatsw1 | last post by:
I need to be certain my application only runs once on a machine, and I implemented a solution using Mutex. The solution was posted many times, but one great link I found is...
1
by: Graham | last post by:
Am I right to assume that you use Mutex(s) for protecting shared data within threads of an application; i.e. its not used to protect shared data between separate applications I have tried to use...
3
by: NaeiKinDus | last post by:
Hello, i'm trying to program a thread that would be locked (by a mutex) and that would only be unlocked once that a function (generating data) is done. The purpose is to generate data, and unlock...
11
by: Lamont Sanford | last post by:
Given an object of type Mutex, what method or property should be called to determine if it is currently owned? I could call WaitOne(0,false) -- and if the return value is false, I could deduce...
45
by: Chris Forone | last post by:
hello group, is there a chance for other functions to get the lock if i have following loop: while (running) { Lock local(mutex); }
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.