views:

239

answers:

5

I'd like to start a new network server project in a language that supports concurrency through fibers aka coroutines aka user-mode threads. Determining what exactly are my options has been exceedingly difficult as the term "coroutine" seems to be used quite loosely to mean a variety of things, and "fiber" is used almost exclusively in reference to the Win32 API.

For the purposes of this question, coroutines/fibers:

  • support methods that pause execution by yielding a result to the calling function from within a nested function (i.e. arbitrarily deep in the call stack from where the coroutine/fiber was invoked)
  • support transferring control to another arbitrary coroutine at its current point of execution (i.e. yield to a coroutine that did not call your coroutine)

What are my language options? I know Ruby 1.9 and Perl (Coro) both have support, what else? Anything with a mature gc and dynamic method invocation is sufficient.

A: 

Stackless Python is another option that meets your requirements. If Python, Ruby and Perl are all unsuitable for your purposes (despite all meeting your stated requirements), you presumably have other unstated requirements or preferences -- care to spell them out?-)

Alex Martelli
I want to evaluate all my options before I choose a language. I don't immediately choose ruby because the GC can be quite slow with large heaps and I don't immediately choose Perl because of it's relatively ugly syntax. That said, Perl is the current frontrunner.
Logan Bowers
Also, do you know if PEP 342 makes regular Python suitable? send() seems like a step in the right direction, but I can't tell if yielding from deep within the call stack is possible.
Logan Bowers
@Logan, a Python generator can only yield to its caller, not to any arbitrary other spot -- to build a coroutine system anyway, one uses a "trampoline" arrangement, as exemplified in Example 3 in PEP 342.
Alex Martelli
A: 

Scheme has call-with-current-continuation which is a building block on which all kinds of flow control can be built. It definitely can support the two uses you mentioned.

There are many robust, widely available implementations of Scheme such as PLT Scheme and Chicken Scheme.

Greg Hewgill
+1  A: 

Lua supports coroutines, see http://lua-users.org/wiki/CoroutinesTutorial , give it a try!

Kknd
After playing with the tutorial code, Lua clearly fails the second requirement.
Logan Bowers
A: 

greenlet extension meets your requirements in Python (regular one, not Stackless).

Greenlet API is a bit low-level, so I recommend using gevent that gives you API suitable for an application. (Disclaimer: I wrote gevent)

Denis Bilenko
A: 

Tcl 8.6, currently in beta, will support coroutines. For more info see the Tcl Wiki coroutine page

Colin Macleod