简体   繁体   English

如何在Lua中使用计时器

[英]how to use a timer in Lua

I capture data with tshark and save certain data from the packet header to process them in order to detect some incedants in the network. 我使用tshark捕获数据,并保存数据包头中的某些数据以进行处理,以检测网络中的某些事件。 I saved the data in a table in my lua program (which is running in the cmd with tshark using the command (-Xlua_script:)) and now i want to process the data of each minute alone while capturing is running. 我将数据保存在lua程序的表中(该表使用tshark在cmd中使用命令(-Xlua_script :)运行),现在我想在捕获运行时单独处理每分钟的数据。 It's an online processing. 这是一个在线处理。 Firstly:Any body knows if this could be implemented?Secondly I need a timer, I don't know how to do this, and i want a way that i can take the data in the tables to process them, reset the tables to get the new data of the next minute without losing any data. 首先:任何人都知道这是否可以实现?其次,我需要一个计时器,我不知道该怎么做,我想要一种方法,可以将表中的数据用于处理它们,重置表以获取下一分钟的新数据而不会丢失任何数据。 Any suggestions or ideas?? 有任何建议或想法吗?

there isn't the concept of a 'timer' in lua like some other languages, where you can create one and set up an event handler and have your main program notified when the timer goes off... however you can periodically check os.clock() to determine how long its been since you've done some processing and if a minute has elapsed, go ahead and process the data. lua中没有像其他语言那样使用“计时器”的概念,您可以在其中创建一个并设置事件处理程序,并在计时器关闭时通知主程序...但是您可以定期检查os。 clock()来确定自从您做了一些处理以来已经有多长时间了,如果一分钟过去了,请继续处理数据。

something like this might be what you need: 这样的事情可能是您需要的:

lastTimeProcessed = os.clock()

function IsTimeToProcess(currentTime)
    span = currentTime - lastTimeProcessed
    if span >= 60 then
        lastTimeProcessed = currentTime
        return true
    end

    return false
end

while true do
    if IsTimeToProcess(os.clock()) then
        -- process some data here
    end
    -- otherwise do another round of whatever you're doing
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM