473,769 Members | 8,283 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Scoped Lock

Hi

There is the Lock object in the threading module.
But there is no medode there I could aquire a scoped
lock like:

mutex = threading.Lock( )
my_lock = mutex.scoped_ac quire() # maybe scoped_lock()
#now this stuff is locked

del mylock

#the lock is released.

def do_domething:
my_lock = mutex.scoped_ac quire()
#now this stuff is locked
#the lock is released after its out of scope
I have written this my own but I'm not sure there is a drawback
because its looks so convinent. So I wonder why its not in
the module?

thx

Marco
Jul 18 '05 #1
9 6182
Marco Bubke wrote:
Hi

There is the Lock object in the threading module.
But there is no medode there I could aquire a scoped
lock like:

mutex = threading.Lock( )
my_lock = mutex.scoped_ac quire() # maybe scoped_lock()
#now this stuff is locked

del mylock

#the lock is released.

def do_domething:
my_lock = mutex.scoped_ac quire()
#now this stuff is locked
#the lock is released after its out of scope
I have written this my own but I'm not sure there is a drawback
because its looks so convinent. So I wonder why its not in
the module?


Some reasons:
- What should happen when an exception happens during the locked stuff?
- It is possible pass a reference to the lock during the locked stuff,
so although the lock goes out of local scope, there still might be
a reference to it.
- The moment that __del__() is called is not guaranteed.

You can also do it like this:

mutex = threading.Lock( )
mutex.acquire()
try:
# now this stuff is locked
finally:
mutex.release() # explicit is better than implicit

Regards,
Ype

email at xs4all.nl
Jul 18 '05 #2
Ype Kingma wrote:
Marco Bubke wrote:
Hi

There is the Lock object in the threading module.
But there is no medode there I could aquire a scoped
lock like:

mutex = threading.Lock( )
my_lock = mutex.scoped_ac quire() # maybe scoped_lock()
#now this stuff is locked

del mylock

#the lock is released.

def do_domething:
my_lock = mutex.scoped_ac quire()
#now this stuff is locked
#the lock is released after its out of scope
I have written this my own but I'm not sure there is a drawback
because its looks so convinent. So I wonder why its not in
the module?


Some reasons:
- What should happen when an exception happens during the locked stuff?
- It is possible pass a reference to the lock during the locked stuff,
so although the lock goes out of local scope, there still might be
a reference to it.
- The moment that __del__() is called is not guaranteed.

You can also do it like this:

mutex = threading.Lock( )
mutex.acquire()
try:
# now this stuff is locked
finally:
mutex.release() # explicit is better than implicit


This does not look nice to me. There should be something me easily.
Maybe that:

def do_something() lock(mutex):
#this stuff is locked by the mutex

So you don't forget to release the lock.

best regards

Marco

Jul 18 '05 #3
Marco Bubke wrote:
This does not look nice to me. There should be something me easily.
Maybe that:

def do_something() lock(mutex):
#this stuff is locked by the mutex

So you don't forget to release the lock.


You could create a class that locks the mutex in the constructor and unlocks
it in the __del__ method.

class ScopedLock:
def __init__ (self, mutex):
self.mutex = mutex
mutex.acquire()

def __del__ (self):
self.mutex.rele ase ()

use as
def do_something():
lock = ScopedLock (mutex)
# do something
# lock will be automatically released when returning
This works only in the current implementation of CPython where local
variables are usually (*) deleted when they fall out of scope.

(*) usually: unless they are returned or added to another object

And I guess with metaclasses, you can achieve something like Java's
synchronized methods.

Daniel

Jul 18 '05 #4
On Mon, Jan 05, 2004 at 01:40:01PM +0100, Daniel Dittmar wrote:
Marco Bubke wrote:
This does not look nice to me. There should be something me easily.
Maybe that:

def do_something() lock(mutex):
#this stuff is locked by the mutex

So you don't forget to release the lock.


You could create a class that locks the mutex in the constructor and unlocks
it in the __del__ method.

class ScopedLock:
def __init__ (self, mutex):
self.mutex = mutex
mutex.acquire()

def __del__ (self):
self.mutex.rele ase ()

use as
def do_something():
lock = ScopedLock (mutex)
# do something
# lock will be automatically released when returning
This works only in the current implementation of CPython where local
variables are usually (*) deleted when they fall out of scope.


Notably, this fails if an exception is raised, since the lock remains in
the locals dictionary of one of the frame objects in the traceback.
Definitely not desirable.

Jp
Jul 18 '05 #5
Ype Kingma <yk*****@access forall.nl> writes:
Marco Bubke wrote:
Hi

There is the Lock object in the threading module.
But there is no medode there I could aquire a scoped
lock like:

mutex = threading.Lock( )
my_lock = mutex.scoped_ac quire() # maybe scoped_lock()
#now this stuff is locked

del mylock

#the lock is released.

def do_domething:
my_lock = mutex.scoped_ac quire()
#now this stuff is locked
#the lock is released after its out of scope
I have written this my own but I'm not sure there is a drawback
because its looks so convinent. So I wonder why its not in
the module?


Some reasons:
- What should happen when an exception happens during the locked stuff?
- It is possible pass a reference to the lock during the locked stuff,
so although the lock goes out of local scope, there still might be
a reference to it.
- The moment that __del__() is called is not guaranteed.

You can also do it like this:

mutex = threading.Lock( )
mutex.acquire()
try:
# now this stuff is locked
finally:
mutex.release() # explicit is better than implicit


This is the way to do it today. There's PEP 310 which, if accepted,
makes this at least shorter to write... (and PEP 310 references a
lengthy discussion about whether using __del__ like this is wise).

Cheers,
mwh

--
3. Syntactic sugar causes cancer of the semicolon.
-- Alan Perlis, http://www.cs.yale.edu/homes/perlis-alan/quotes.html
Jul 18 '05 #6
Marco Bubke wrote:
Ype Kingma wrote:
Marco Bubke wrote:
Hi

There is the Lock object in the threading module.
But there is no medode there I could aquire a scoped
lock like:

mutex = threading.Lock( )
my_lock = mutex.scoped_ac quire() # maybe scoped_lock()
#now this stuff is locked

del mylock

#the lock is released.

def do_domething:
my_lock = mutex.scoped_ac quire()
#now this stuff is locked
#the lock is released after its out of scope
I have written this my own but I'm not sure there is a drawback
because its looks so convinent. So I wonder why its not in
the module?


Some reasons:
- What should happen when an exception happens during the locked stuff?
- It is possible pass a reference to the lock during the locked stuff,
so although the lock goes out of local scope, there still might be
a reference to it.
- The moment that __del__() is called is not guaranteed.

You can also do it like this:

mutex = threading.Lock( )
mutex.acquire()
try:
# now this stuff is locked
finally:
mutex.release() # explicit is better than implicit


This does not look nice to me. There should be something me easily.


An elegant and easier way has been designed, see PEP 310,
mentioned in another post in this thread:
http://www.python.org/peps/pep-0310.html

However, one normally does not use locks so often that the
lightly verbose idiom needed to use them becomes a problem.

Regards,
Ype

Jul 18 '05 #7
How about:

def lock_call(mutex , func, *args, **kwargs):
mutex.acquire()
try:
res = func(*args, **kwargs)
finally:
mutex.release()

return res

def do_something(bl ah):
# do stuff

m = threading.Lock
lock_call(m, do_something, "some data")
Jul 18 '05 #8
Sean R. Lynch wrote:
How about:

def lock_call(mutex , func, *args, **kwargs):
mutex.acquire()
try:
res = func(*args, **kwargs)
finally:
mutex.release()

return res


Or even:

def lock_call(mutex , func, *args, **kwargs):
mutex.acquire()
try:
return func(*args, **kwargs)
finally:
mutex.release()

Regards,
Ype

Jul 18 '05 #9
Michael Hudson wrote:
Ype Kingma <yk*****@access forall.nl> writes:
Marco Bubke wrote:
> Hi
>
> There is the Lock object in the threading module.
> But there is no medode there I could aquire a scoped
> lock like:
>
> mutex = threading.Lock( )
> my_lock = mutex.scoped_ac quire() # maybe scoped_lock()
> #now this stuff is locked
>
> del mylock
>
> #the lock is released.
>
> def do_domething:
> my_lock = mutex.scoped_ac quire()
> #now this stuff is locked
> #the lock is released after its out of scope
>
>
> I have written this my own but I'm not sure there is a drawback
> because its looks so convinent. So I wonder why its not in
> the module?


Some reasons:
- What should happen when an exception happens during the locked stuff?
- It is possible pass a reference to the lock during the locked stuff,
so although the lock goes out of local scope, there still might be
a reference to it.
- The moment that __del__() is called is not guaranteed.

You can also do it like this:

mutex = threading.Lock( )
mutex.acquire()
try:
# now this stuff is locked
finally:
mutex.release() # explicit is better than implicit


This is the way to do it today. There's PEP 310 which, if accepted,
makes this at least shorter to write... (and PEP 310 references a
lengthy discussion about whether using __del__ like this is wise).


Thats looks really nice.

thx

Marco

Jul 18 '05 #10

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

Similar topics

0
2254
by: Cruinne | last post by:
Hello, In a JBoss 3.2.4 server, I have deployed several enterprise javabeans (each as a separate .jar, not in an .ear bundle). These beans are completely self-contained, as they include all their needed dependencies inside the jar files (and have a reference to them in the Class-Path entry in their MANIFEST.MF). The problem shows when I want these beans to be scoped, this is, to have their own classloader inside JBoss and not use the...
3
1992
by: Wayfarer | last post by:
Hi, I'm trying to set up a simple application-scoped counter in global.asa to display on my webpages. When I call Application("VisitorCount") on another page as shown: <% response.write "Visitor: " & Application("VisitorCount") %>
4
1894
by: Otto Lind | last post by:
The following program shows the problem: #include <stdio.h> namespace x { enum L { One = 1, Two = 2 }; namespace x { enum L { Three = 3, Four = 4 };
0
963
by: suranga.suranga | last post by:
Hi, I am somewhat confused about why "application-scoped settings" are readonly, and cannot be updated at runtime. I have an application which is run by users, and in unattended mode under a system account. I would like all the users to share the same database connection string, which can be updated by any user. When this is run in unattended mode as a windows scheduled task under a system account, it has to be able to access...
3
4940
by: Mirek Endys | last post by:
Hello all, I have this problem. I have a windows service and I would like to put there Settings for starting this service. Because of this service is running under NETWORK_SERVICE account, what is the best way to use settings? Should I create my own settings or can I use classic application settings. Is there way to change this application-scoped settings change programmatically? Thanks.
0
3403
by: Cyke | last post by:
Hello, I have created an ASP.NET application that consumes a C# webservice. The webservice works fine on its own. However, when I try to access it from my applicaiton, I get the following error: "The current configuration system does not support user-scoped settings" I don't have any user-scoped settings defined in my application or my
2
12833
by: Dave Booker | last post by:
How are complex data types saved and restored when used as User-scoped Application Settings? For an example of what I'm trying to do that does not work, take the following: A project has a user-scoped Settings object called "TestStrings" which is of type "StringDictionary". The following code is run: if (Settings.Default.TestStrings == null) Settings.Default.TestStrings = new StringDictionary();...
1
1445
dmjpro
by: dmjpro | last post by:
Is there any term by this name exported scoped variables in JSP??? PLz explain. pageContext,page,sessionScope,requestScope r these Scoped Variables in JSP?? Kind regards, DMJPRO.
2
1700
by: Chris Dunaway | last post by:
What what I have read, Application Scoped settings in the setting file are read only. Why is this? Is there a good reason for this restriction? I understand that some settings will be different for each user of the app, hence the User Scoped settings. User scoped settings can be changed and persisted at runtime. But there are also settings that are not User Scoped (i. e. Application Scoped) that still might need to be changed at...
0
10049
jinu1996
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...
1
9998
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,...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7413
isladogs
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...
0
6675
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();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3567
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
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...

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.