【问题标题】:Creating a timer using Lua使用 Lua 创建计时器
【发布时间】:2011-05-25 02:07:49
【问题描述】:

我想使用 Lua 创建一个计时器,我可以指定一个回调函数在 X 秒后触发。

实现这一目标的最佳方法是什么? (我需要从网络服务器下载一些数据,每小时解析一次或两次)

干杯。

【问题讨论】:

    标签: timer lua


    【解决方案1】:

    如果不需要毫秒精度,您可以选择协程解决方案,定期恢复,就像在主循环结束时一样,像这样:

    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
    

    这使用 LuaSocket 中的 sleep 功能,您可以使用Lua-users Wiki 上建议的任何其他替代方法

    【讨论】:

      【解决方案2】:

      试试lalarm,在这里: http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/

      示例(基于 src/test.lua):

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

      输出:

      1
      2
      

      【讨论】:

      • 你能给我举个例子,说明如何以简单的方式使用闹钟每分钟调用一次函数吗?
      • 你看代码了吗? lalarm 存档中有一个文件“test.lua”。
      • 不能连续设置两个闹钟,否则第二个会覆盖第一个。
      【解决方案3】:

      如果你可以接受,你可以试试LuaNode。以下代码设置了一个计时器:

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

      【讨论】:

        【解决方案4】:

        使用 Script.SetTimer(interval, callbackFunction)

        【讨论】:

          【解决方案5】:

          在阅读了这个帖子和其他帖子后,我决定使用Luv lib。这是我的解决方案:

          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
          

          【讨论】:

            【解决方案6】:

            在我的 Debian 上,我安装了 lua-lgi 数据包以访问基于 GObject 的库。

            以下代码向您展示了一个用法,证明您可以使用少量异步回调:

            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")
            

            你可以找到more documentation about LGI here

            【讨论】:

              【解决方案7】:

              我知道这有点便宜,但我使用 JS,并且对 Luna 没有什么经验。所以这是我唯一不使用库的解决方案:

              for i = 1,0,0 do
                 wait(5)
                 print("Hello Whoever Is Reading This!")
              end
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2021-04-09
                • 2012-10-13
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多