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.__resource s = 100
def getResource(sel f):
BEGIN CRITICAL SECTION HERE
if self.__resource s > 0:
self.__resource s -= 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. 2 5414
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.__resource s = 100
def getResource(sel f): BEGIN CRITICAL SECTION HERE if self.__resource s > 0: self.__resource s -= 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
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.pu t(None)
def getResource(sel f):
self.__queue.ge t()
do my thing
self.__queue.pu t(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.pu t(item)
def apply(self, func):
item = self.__queue.ge t()
result = func(item)
self.__queue.pu t(item)
return result
manager = resourceManager ([blackboard])
...
manager.apply(w rite_on_blackbo ard)
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 speak only of features in the spirit of C; something like
object-orientation, though a nice feature, does not belong in C.
Something like being able to #define a #define would be very handy,
though, e.g:
#define DECLARE_FOO(bar) #define...
|
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 Mutex mtxBezoeken = new Mutex(false, "bezoeken");
which compiles and executes just fine. Then in another page I want to write
to a file that may also be accessed by global.asax.cs so I want access to
|
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. It is possible for the user to generate an error and be able to "fix" the thing that caused the error (perhaps by changing some parameter) and then want to run the operation again from the same process. But if there's been an error, then he's out...
|
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... next) from the global
collection. I'm getting an error when the worker threads try to read
from the collection while the main thread is either adding or removing
items on the global collection. On the main thread I've tried using a
synclock when...
|
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
http://www.yoda.arachsys.com/csharp/faq/#one.application.instance
The problem is, when I switch users (XP Professional) the second user can
open a second instance of the application! How can I prevent this?
Here's the relevant part of my code:
| |
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 named Mutex's to protect shared data between two separate applications, but not clear how/where to create the Mutex's. If I create the Mutex once in the InitDialog() routine in each application then I never return from a WaitForSIngleObject(), or if I...
|
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
the mutex in order to activate the thread once the data is generated.
I have to do it this way, i can only call the thread if the data are
generated.
********************************************************
step 1: initialize the mutex
|
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 that the Mutex is already owned... but this has the unwanted side
effect of seizing ownership of the Mutex in the case where the Mutex is NOT
already owned.
I'm looking for something like an "IsOwned" property.
|
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);
}
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |