2

I use NoSQL database Tarantool and try to do some complex work on DB side using Lua stored procedures. I think it`s a good idea, because i can do less DB calling and have less overhead with network data transfer.
I have some table:
user_counters: id, counter_a, counter_b, score

And, for example, i have some function to calculate field 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

And i have another function for fields counter_a and counter_b update:

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

How can i call recalc_score(id) function and return user_counters_tuple without waiting when previous function execution will be finished?

1 Answers1

2

Just use fiber.create(fun, ...):

local fiber = require('fiber')

-- start another coroutine recalc_score(id) and forget about it
fiber.create(recalc_score, id)
Roman T.
  • 86
  • 1