实际使用Lua协同例程/继续序列化来简化异步逻辑?

Lil*_*ver 5 continuations lua serialization asynchronous continuation-passing

冥王星库Lua中声称能够序列化的Lua协程.我将其解释为'serializeable continuations',这是使异步编程以同步方式可写的重要特性.

例如,工作流可以线性表示,而不是需要命名的入口点

if (ask user is hungry) then
     if (not ask user is vegetarian) then
       if (ask user if likes_burgers) then
          feed_user(burger)
       else
          tell_user("Picky!")
     else
       feed_user(salad)
Run Code Online (Sandbox Code Playgroud)

代替

function main()
   ask(user is hungry, "hungry_response")

function hungry_response(answer)
  if (answer is yes)
     ask(user is vegetarian, "vegetarian_response")

function vegetarian_response(answer)
  if (answer is yes)
     feed_user(salad)
  else
     ask(user likes burgers, "burgers_response")

function burgers_response(answer)
  if (answer is yes) then
    feed_user(burger)
  else
    tell_user("Picky!")
Run Code Online (Sandbox Code Playgroud)

虽然翻译成前一个样式的if语句也不错,但是一旦涉及局部变量,循环,嵌套函数调用等,事情就变得非常复杂.

这是序列化延续变得至关重要的地方.

序列化的延续在JavaFlow,Cocoon(Rhink),Seaside,PLT Scheme,SICS中使用,非常适合处理业务工作流程,医疗诊断和(在我的情况下)文本冒险游戏.

是否有任何Lua和Pluto以这种方式利用其功能的示例(希望是开源!),使用continuation来简化异步环境中的逻辑?

小智 3

http://sheddingbikes.com/posts/1289384533.html

http://dpaste.de/Huj4/

例如,查看 WKP(“知名程序员”)中的 Tir。它简化了(如果不是序列化的话)异步操作。它是一个 BSD 许可的微型 Web 框架,使用 Lua 协程。

从博客...

让这项工作成功的神奇之处在于 Lua 的协程。在我们调用 web:prompt 和 web:click 的每个点上,Tir 引擎都会生成我们的处理程序,保存它供以后使用,然后新的请求将其返回。执行此操作的代码基本上是:

function Web:recv()
    return coroutine.yield()
end
Run Code Online (Sandbox Code Playgroud)