Updated at July 17, 2026 02:08 PM
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 fiber.info()fiber.name('Respawn fiber')while true dofor _, tuple in box.space.pokemons.index.status:pairs(self.state.CAUGHT) dobox.space.pokemons:update(tuple[self.ID],{{'=', self.STATUS, self.state.ACTIVE}})endfiber.sleep(self.respawn_time)endend
and call it as a fiber in start():
start = function(self)-- create spaces and indexes<...>-- create models<...>-- compile models<...>-- start the gameself.pokemon_model = compiled_pokemonself.player_model = compiled_playerfiber.create(self.respawn, self)log.info('Started')-- errors if schema creation or compilation fails<...>end