What's the advantage of stack-less Python's microthread than Lua's coroutine in state machine implementation for game?

StackOverflow https://stackoverflow.com/questions/4064594

Question

Any advantage on stack-less python implentation than Lua's coroutine? What's the difference of them?

Was it helpful?

Solution

stackless python and tasklets (I haven't done any programming with stackless python, but I have read some of the details about how it is implemented):

Pros:

  1. Lightweight most of the time.
  2. Has scheduler to manage which tasklet get resume next after the current one yields.
  3. Support for Preemptive Scheduling. (i.e. run for X instructions)
  4. Channels for communication between tasklets.

Cons:

  1. Sometimes need C-stack when yielding from a tasklet. (i.e. when yielding from some C callbacks)

Lua 5.1 with plain coroutines:

Pros:

  1. Lightweight.
  2. resume()/yield() functions allow consumer/producer model of communication.

Cons:

  1. No built-in scheduler. You have to manage resuming & yielding coroutines.
  2. Can't yield from a C function, a metamethod, or an iterator. (Lua 5.2 will remove most of these restrictions, LuaJIT 1.1 provides lightweight c-stack switching to yield from anywhere)
  3. No built-in Preemptive Scheduling support. (would have to use debug hooks)

Lua 5.1 with ConcurrentLua:

Pros:

  1. Lightweight.
  2. Scheduler with cooperative context switching.
  3. Has Erlang style of message passing communication between tasks.
  4. Support for transparent distributed message passing between nodes.

Cons:

  1. Can't yield from a C function, a metamethod, or an iterator. (again most of these restrictions go-away with Lua 5.2 and LuaJIT)
  2. No built-in Preemptive Scheduling support. (would have to use debug hooks)

LuaJIT 2.0 Beta with ConcurrentLua:

Pros:

  1. Lightweight.
  2. Scheduler with cooperative context switching.
  3. Has Erlang style of message passing communication between tasks.
  4. Support for transparent distributed message passing between nodes.
  5. Very fast JIT support makes Lua much faster then Python

Cons:

  1. Might not be able to yield from a C function right now. This might be supported in future releases.
  2. No built-in Preemptive Scheduling support. (would have to use debug hooks)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top