serialization - 实际使用 Lua 协程/延续序列化来简化异步逻辑?

标签 serialization asynchronous lua continuations continuation-passing

Pluto libraryLua声称能够序列化 Lua 协程。我将其解释为“可序列化的延续”,这是使异步编程以同步风格可写的重要特性。

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

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)

代替
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!")

虽然 if 语句转换成以前的风格还不错,但一旦涉及局部变量、循环、嵌套函数调用等,事情就会变得非常复杂。

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

Serialized continuations用于 JavaFlow、Cocoon (Rhink)、Seaside、PLT Scheme、SICS,非常适合处理业务工作流、医疗诊断和(在我的情况下)文本冒险游戏。

有没有 Lua 和 Pluto 以这种方式利用它们的特性,使用延续来简化异步环境中的逻辑的例子(希望是开源的!)?

最佳答案

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

http://dpaste.de/Huj4/

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

从博客...

The magic that makes this work is Lua's coroutines. At each of the points where we call web:prompt and web:click the Tir engine yields our handler, saves it for later, and then a new request brings it back. Code that does this is basically:

function Web:recv()
    return coroutine.yield()
end

关于serialization - 实际使用 Lua 协程/延续序列化来简化异步逻辑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5234514/

相关文章:

serialization - 序列化和计数值列表

c# - 为什么在任务中运行的 Thread.Sleep 不会阻塞 WinForms UI 线程?

variables - Rbx.Lua - 为什么我不能将 .txt 文件存储为表格?

Lua字符串转int

java - 异步任务和程序流程遇到问题

lua - nodemcu string.format 奇数结果

java - java.util.Optional为什么不是Serializable,如何用这样的字段序列化对象

python - Django Rest Framework - 嵌套序列化程序是延迟加载的吗?

php - FOSElastica + JMs 序列化程序格式错误的数据

c# - 将 UI 线程的 TaskScheduler 存储在字段中是否安全?