Freeze and Resume execution

Miki Tebeka miki.tebeka at zoran.com
Sun May 23 09:13:53 EDT 2004


Hello Slawomir,

> 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
> 

My problem is that the one raising the error is the generator. And I'd
like to resume the generator just after the exception was raised.
Something in the lines of:

class BufferFull(Exception):
    pass

def send_one_bit(bit):
    pass

OUT_BUFFER_SIZE = 10
def output(bits):
    size = 0
    for bit in bits:
        send_one_bit(bit) # HW call
        size += 1
        if size == OUT_BUFFER_SIZE:
            raise BufferFull # I'd like to resume just after this point
            size = 0

Note that the output buffer is called about 3-5 stack frames down from
the function catching the exception.

Bye.
--
-------------------------------------------------------------------------
Miki Tebeka <miki.tebeka at zoran.com>
http://www.cs.bgu.ac.il/~tebeka
The only difference between children and adults is the price of the toys.




More information about the Python-list mailing list