RfD: Automagically making a class instance 'synchronized' (draft impl.)
holger krekel
pyth at devel.trillke.net
Tue Jun 11 18:04:25 EDT 2002
Gerhard H?ring wrote:
> For one of my projects, I'll need something not quite unlike the
> following code. It's not tested, yet, but it looks ok and complete to
> me. I thought that it could be useful to other developers who need to
> use multithreading, so I'm posting it here. Maybe something like this
> would even be a useful addition to the Standard Library?
note that Ian Kjos some weeks ago send a nice module 'morethreading.py'
to the dev-list. IIRC, it still waits for beeing reviewed and extended
with other ideas (like synchronized methods).
> from threading import RLock
> ...
> def __call__(self, *args, **kwargs):
> self.proxy.__dict__["rlock"].acquire(1)
> ...
I think for inclusion into the standard lib it *might* be
worth to reduce overhead (doubly-indirect function calls + lookups).
A quick google search showed there has already been some discussion
on this issue and there seems to be a 'synchronizing' metaclass.
anyway, another comment:
> try:
> self.obj(*args, **kwargs)
> except:
> self.proxy.__dict__["rlock"].release()
> raise
> return
> self.proxy.__dict__["rlock"].release()
this can be safely substituted and then even returns the correct value<wink> with
try:
return self.obj(*args, **kwargs)
finally:
self.proxy.__dict__["rlock"].release()
the finally statement is executed in all cases and doesn't hinder
an exception to be raised eventually. I think it was designed
for use cases exactly like this :-)
regards,
holger
More information about the Python-list
mailing list