Een timer maken met Lua

Ik wil graag een timer maken met Lua, op een manier dat ik een callback-functie kan specificeren die moet worden geactiveerd nadat er X seconden zijn verstreken.

Wat is de beste manier om dit te bereiken? ( Ik moet wat gegevens downloaden van een webserver die een of twee keer per uur wordt geparseerd )

Proost.


Antwoord 1, autoriteit 100%

Probeer lalarmhier:
http://www.tecgraf.puc-rio.br/~lhf /ftp/lua/

Voorbeeld (gebaseerd op src/test.lua):

-- alarm([secs,[func]])
alarm(1, function() print(2) end); print(1)

Uitvoer:

1
2

Antwoord 2, autoriteit 90%

Als nauwkeurigheid in milliseconden niet nodig is, kunt u gewoon voor een coroutine-oplossing gaan, die u periodiek hervat, zoals aan het einde van uw hoofdlus, zoals deze:

require 'socket' -- for having a sleep function ( could also use os.execute(sleep 10))
timer = function (time)
    local init = os.time()
    local diff=os.difftime(os.time(),init)
    while diff<time do
        coroutine.yield(diff)
        diff=os.difftime(os.time(),init)
    end
    print( 'Timer timed out at '..time..' seconds!')
end
co=coroutine.create(timer)
coroutine.resume(co,30) -- timer starts here!
while coroutine.status(co)~="dead" do
    print("time passed",select(2,coroutine.resume(co)))
    print('',coroutine.status(co))
    socket.sleep(5)
end

Hiermee wordt de slaapfunctie in LuaSocket gebruikt, u kunt elk ander alternatief gebruiken dat wordt gesuggereerd op de Lua-users Wiki


Antwoord 3, autoriteit 20%

Als het voor jou acceptabel is, kun je LuaNodeproberen. De volgende code stelt een timer in:

setInterval(function()
    console.log("I run once a minute")
end, 60000)
process:loop()

Antwoord 4

gebruik Script.SetTimer(interval, callbackFunction)


Antwoord 5

Na het lezen van deze thread en andere heb ik besloten om voor Luvlib te gaan. Hier is mijn oplossing:

uv = require('luv') --luarocks install luv
function set_timeout(timeout, callback)
  local timer = uv.new_timer()
  local function ontimeout()
    uv.timer_stop(timer)
    uv.close(timer)
    callback()
  end
  uv.timer_start(timer, timeout, 0, ontimeout)
  return timer
end
set_timeout(1000, function() print('ok') end) -- time in ms
uv.run() --it will hold at this point until every timer have finished

Antwoord 6

Op mijn Debian heb ik lua-lgi-pakket geïnstalleerd om toegang tot de GObject-gebaseerde bibliotheken.

De volgende code toont u een gebruik dat aantoont dat u enkele asynchrone callbacks kunt gebruiken:

local lgi = require 'lgi'
local GLib = lgi.GLib
-- Get the main loop object that handles all the events
local main_loop = GLib.MainLoop()
cnt = 0
function tictac()
     cnt = cnt + 1
     print("tic")
     -- This callback will be called until the condition is true
     return cnt < 10
end
-- Call tictac function every 2 senconds
GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, 2, tictac)
-- You can also use an anonymous function like that
GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, 1,
                         function()
                            print( "There have been ", cnt, "tic")
                            -- This callback will never stop
                            return true
                         end)
-- Once everything is setup, you can start the main loop
main_loop:run()
-- Next instructions will be still interpreted
print("Main loop is running")

U kunt meer documentatie over LGI hier

Other episodes