Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scripting languages that support fibers/coroutines?

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.

like image 472
Logan Bowers Avatar asked Dec 24 '09 02:12

Logan Bowers


2 Answers

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)

like image 172
Denis Avatar answered Oct 08 '22 00:10

Denis


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

like image 25
Kknd Avatar answered Oct 08 '22 02:10

Kknd