[Python-Dev] functional continuations

tomer filiba tomerfiliba at gmail.com
Sat Dec 15 10:04:34 CET 2007


i'm working on some minimalistic asynchronous framework for python,
somewhat like twisted or stackless, but for different purposes. i came
to the conclusion i want to be able to "freeze" functions, and resume
them later, when some condition is matched.

the idea i came up with is, using exceptions for functional
continuations: after all, the exception's traceback holds the entire
context... so with some help from black magic (i.e., ctypes), i can
resurrect dead stackframes. i will have a reactor in the background
which manages the execution flow, and blocking operations (I/O, etc.)
will be wrapped with a thin wrapper, such as

class socket2(socket):
    def recv(self, count = None):
        raise WaitFor(self, "r")
        return socket.recv(self, count)

now, when read()ing from file2 objects, first an exception will
propagate up to the reactor, which will catch it, create a
continuation object (which holds the traceback) and register it with
the desired event.

then, when the event is triggered, the reactor will resurrect the
continuation object to the point where it stopped (f_lasti + 2), and
execution would continue normally from there.

the idea itself is similar to generator objects, and requires some
messing-around with f_back and f_lasti which is quite ugly. on the
other hand, the beauty of it is, it doesn't require any special design
patterns (unlike twisted) or replacing the interpreter (unlike
stackless), so it can be transparently used with any third-party libs.
all it takes is wrapping I/O objects to "raise WaitFor(...)" prior to
doing blocking I/O.

moreover, unlike generators, i gain the ability to "propagate" up to
the reactor (like normal exceptions), so the code in between the I/O
and the reactor needn't be aware of anything (only avoiding
unconstrained try-except blocks).

i wanted to get some feedback on the issue (i tried c.l.p, but they
didn't understand me well enough):
* has it been tried before?
* do you suppose it will work? are there any drawbacks i didn't
anticipate?
* is it useful enough to be added to the core interpreter (like
generators)? of course if it is added, it could be implemented with a
dedicated mechanism, rather than abusing exceptions for that.


-tomer


More information about the Python-Dev mailing list