【问题标题】:Run Tarantool Lua function in another coroutine在另一个协程中运行 Tarantool Lua 函数
【发布时间】:2016-01-02 11:05:52
【问题描述】:

我使用 NoSQL 数据库 Tarantool 并尝试使用 Lua 存储过程在 DB 端做一些复杂的工作。我认为这是一个好主意,因为我可以减少数据库调用,并且减少网络数据传输的开销。
我有一些表:
user_counters:id、counter_a、counter_b、score

而且,例如,我有一些函数来计算字段分数:

function recalc_score(id)
   local stream = box.space.user_counters:select { id }
   local rating = 0
   -- some_rating_calculation using counter_a and counter_b here
   box.space.user_counters:update(id, { { '=', 4, rating } })
end

我还有另一个功能用于字段 counter_a 和 counter_b 更新:

function update_user_counters(id, counter_a_diff, counter_b_diff)
    local rating_default = 0
    local user_counters_tuple = box.space.user_counters:upsert(
        { id, counter_a_diff, counter_b_diff, rating_default },
        { { '+', 2, counter_a_diff }, { '+', 3, counter_b_diff } }
    )
    -- start another coroutine recalc_score(id) and forget about it
    return user_counters_tuple
 end

如何调用 recalc_score(id) 函数并返回 user_counters_tuple 而无需等待前一个函数执行完成?

【问题讨论】:

    标签: lua tarantool


    【解决方案1】:

    只需使用 fiber.create(fun, ...):

    local fiber = require('fiber')
    
    -- start another coroutine recalc_score(id) and forget about it
    fiber.create(recalc_score, id)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-25
      • 2017-09-19
      • 2020-07-05
      • 2012-04-03
      • 1970-01-01
      • 2018-05-10
      相关资源
      最近更新 更多