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_acquire() # maybe scoped_lock()
#now this stuff is locked
del mylock
#the lock is released.
def do_domething:
my_lock = mutex.scoped_acquire()
#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 9 6097
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_acquire() # maybe scoped_lock() #now this stuff is locked
del mylock
#the lock is released.
def do_domething: my_lock = mutex.scoped_acquire() #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
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_acquire() # maybe scoped_lock() #now this stuff is locked
del mylock
#the lock is released.
def do_domething: my_lock = mutex.scoped_acquire() #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
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.release ()
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
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.release ()
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
Ype Kingma <yk*****@accessforall.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_acquire() # maybe scoped_lock() #now this stuff is locked
del mylock
#the lock is released.
def do_domething: my_lock = mutex.scoped_acquire() #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
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_acquire() # maybe scoped_lock() #now this stuff is locked
del mylock
#the lock is released.
def do_domething: my_lock = mutex.scoped_acquire() #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
How about:
def lock_call(mutex, func, *args, **kwargs):
mutex.acquire()
try:
res = func(*args, **kwargs)
finally:
mutex.release()
return res
def do_something(blah):
# do stuff
m = threading.Lock
lock_call(m, do_something, "some data")
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
Michael Hudson wrote: Ype Kingma <yk*****@accessforall.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_acquire() # maybe scoped_lock() > #now this stuff is locked > > del mylock > > #the lock is released. > > def do_domething: > my_lock = mutex.scoped_acquire() > #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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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 };
|
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...
|
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...
|
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...
|
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...
|
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.
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |