Am Samstag, 2. April 2005 22:28 schrieb Paul Rubin: > I'm starting to believe the GIL covers up an awful lot of sloppiness > in Python. I wonder if there could be a decorator approach: > > @synchronized > def counter(): > t = itertools.count() > while True: > yield t.next()
Of course there could:
def synchronized_iterator(f):
def wrapper(*args,**kwargs):
class iterator(object):
def __init__(self,f,args,kwargs):
self.iter = f(*args,**kwargs)
self.lock = threading.RLock()
def __iter__(self):
return self
def next(self):
self.lock.acquire()
try:
return self.iter.next()
finally:
self.lock.release()
return iterator(f,args,kwargs)
return wrapper
@synchronized_iterator
def create_counter():
t = itertools.count()
while True:
yield t.next()
or
counter = synchronized_iterator(itertools.count)
I used a class-based approach, as I don't want to destroy the semantics of
calling the returned wrapper(), which should've already instantiated the
wrapped generator object (which doesn't happen when making wrapper() a
generator itself).
--
--- Heiko.
pgp4kxA2GZNHL.pgp
Description: PGP signature
-- http://mail.python.org/mailman/listinfo/python-list
