Accessing a shared generator from multiple threads.

Aahz aahz at pythoncraft.com
Wed Jan 21 19:53:03 EST 2004


In article <20040121131300.D8E9.JCARLSON at uci.edu>,
Josiah Carlson  <jcarlson at uci.edu> wrote:
>
>Of course you could just as easily use a single lock and a class:
>
>class lockedgen:
>    def __init__(self, gen):
>        self.g = gen
>        self.l = threading.Lock()
>    def get(self):
>        self.l.acquire()
>        a = self.g.next()
>        self.l.release()
>        return a

This is one case where you *DEFINITELY* want to use try/finally:

class lockedgen:
    def __init__(self, gen):
        self.g = gen
        self.l = threading.Lock()
    def get(self):
        self.l.acquire()
        try:
            a = self.g.next()
        finally:
            self.l.release()
        return a
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

A: No.
Q: Is top-posting okay?



More information about the Python-list mailing list