Fibers, yields and cooperative multitasking | Tarantool
Документация на русском языке
поддерживается сообществом

Fibers, yields and cooperative multitasking

But wait! If we launch it as shown above – self.respawn() – the function will be executed only once, just like all the other methods. But we need to execute respawn() every 60 seconds. Creating a fiber is the Tarantool way of making application logic work in the background at all times.

A fiber is a set of instructions that are executed with cooperative multitasking: the instructions contain yield signals, upon which control is passed to another fiber.

Let’s launch respawn() in a fiber to make it work in the background all the time. To do so, we’ll need to amend respawn():

respawn = function(self)
    -- let's give our fiber a name;
    -- this will produce neat output in'Respawn fiber')
    while true do
        for _, tuple in
                self.state.CAUGHT) do
                {{'=', self.STATUS, self.state.ACTIVE}}

and call it as a fiber in start():

start = function(self)
    -- create spaces and indexes
    -- create models
    -- compile models
    -- start the game
       self.pokemon_model = compiled_pokemon
       self.player_model = compiled_player
       fiber.create(self.respawn, self)'Started')
    -- errors if schema creation or compilation fails
Нашли ответ на свой вопрос?
Обратная связь