Freeze and Resume execution

Slawomir Nowaczyk slawek at cs.lth.se
Sun May 23 06:11:17 EDT 2004


On Sun, 23 May 2004 12:16:20 +0200
"Miki Tebeka" <miki.tebeka at zoran.com> wrote:

#>>> I'd like to use generators but can't see any "nice" way of doing
#>>> it.

#>> That would be the approach I would take. What can't you see with
#>> using yield this way?

#> The output function is way down the call stack. The function that
#> handles the "freezing" and need to be saved is about 3-5 stack
#> frames up (depends on the scenario).

#> Propagating the output return value all the way back is tedious and
#> error prone. This is why I'd like to use raise/catch and not "if
#> error ..."

I must admit I don't really understand what your problem is, so my
solution may not be what you are looking for. However, maybe it will
be useful:

**********************************************************************

def generator():
    i = 0
    while 1:
        i += 1
        yield i

class A:
    def __init__(self):
        self.gen = generator()
    def work(self):
        i = 0
        while True:
            print self.gen.next()
            i += 1
            if i > 5:
                raise ValueError

a = A()

def x():
    a.work()

def y():
    x()

def z():
    for i in range(5):
        try:
            y()
        except ValueError:
            print "do something with this exception"
    
**********************************************************************

-- 
 Best wishes,
   Slawomir Nowaczyk
     ( Slawomir.Nowaczyk at cs.lth.se )

Get the facts first - you can distort them later!





More information about the Python-list mailing list