What is the difference between a coroutine and a continuation and a generator ?
I’ll start with generators, seeing as they’re the simplest case. As @zvolkov mentioned, they’re functions/objects that can be repeatedly called without returning, but when called will return (yield) a value and then suspend their execution. When they’re called again, they will start up from where they last suspended execution and do their thing again.
A generator is essentially a cut down (asymmetric) coroutine. The difference between a coroutine and generator is that a coroutine can accept arguments after it’s been initially called, whereas a generator can’t.
It’s a bit difficult to come up with a trivial example of where you’d use coroutines, but here’s my best try. Take this (made up) Python code as an example.
def my_coroutine_body(*args): while True: # Do some funky stuff *args = yield value_im_returning # Do some more funky stuff my_coro = make_coroutine(my_coroutine_body) x = 0 while True: # The coroutine does some funky stuff to x, and returns a new value. x = my_coro(x) print x
An example of where coroutines are used is lexers and parsers. Without coroutines in the language or emulated somehow, lexing and parsing code needs to be mixed together even though they’re really two separate concerns. But using a coroutine, you can separate out the lexing and parsing code.
(I’m going to brush over the difference between symmetric and asymmetric coroutines. Suffice it to say that they’re equivalent, you can convert from one to the other, and asymmetric coroutines–which are the most like generators–are the easier to understand. I was outlining how one might implement asymmetric coroutines in Python.)
Continuations are actually quite simple beasts. All they are, are functions representing another point in the program which, if you call it, will cause execution to automatically switch to the point that function represents. You use very restricted versions of them every day without even realising it. Exceptions, for instance, can be thought of as a kind of inside-out continuation. I’ll give you a Python based pseudocode example of a continuation.
Say Python had a function called
callcc(), and this function took two arguments, the first being a function, and the second being a list of arguments to call it with. The only restriction on that function would be that the last argument it takes will be a function (which will be our current continuation).
def foo(x, y, cc): cc(max(x, y)) biggest = callcc(foo, [23, 42]) print biggest
What would happen is that
callcc() would in turn call
foo() with the current continuation (
cc), that is, a reference to the point in the program at which
callcc() was called. When
foo() calls the current continuation, it’s essentially the same as telling
callcc() to return with the value you’re calling the current continuation with, and when it does that, it rolls back the stack to where the current continuation was created, i.e., when you called
The result of all of this would be that our hypothetical Python variant would print
I hope that helps, and I’m sure my explanation can be improved on quite a bit!
Coroutine is one of several procedures that take turns doing their job and then pause to give control to the other coroutines in the group.
Continuation is a “pointer to a function” you pass to some procedure, to be executed (“continued with”) when that procedure is done.
Generator (in .NET) is a language construct that can spit out a value, “pause” execution of the method and then proceed from the same point when asked for the next value.
In newer version of Python, you can send values to Generators with
generator.send(), which makes python Generators effectively coroutines.
The main difference between python Generator, and other generator, say greenlet, is that in python, your
yield value can only return back to the caller. While in greenlet,
target.switch(value) can take you to a specific target coroutine and yield a value where the
target would continue to run.