加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

最简单的方法让lua脚本等待/暂停/睡眠/阻塞几秒钟?

发布时间:2020-12-15 00:19:21 所属栏目:大数据 来源:网络整理
导读:我不知道如何让lua做任何常见的计时技巧,如 sleep – 停止线程上的所有操作 暂停/等待 – 不要去下一个 命令,但允许其他代码 应用程序继续 block – 不要去下一个命令,直到 当前一个返回 我读过一个 while os.clock()time_point do --nothingend 占用CPU
我不知道如何让lua做任何常见的计时技巧,如

> sleep – 停止线程上的所有操作
>暂停/等待 – 不要去下一个
命令,但允许其他代码
应用程序继续
> block – 不要去下一个命令,直到
当前一个返回

我读过一个

while os.clock()<time_point do 
--nothing
end

占用CPU时间。

有什么建议么?有没有一个API调用我错过了?

更新:我写了这个问题很久以前试图让WOW Lua按照时间表重播行动(即站立,等待1秒,跳舞,等待2秒,坐。没有停顿,这些发生几乎所有在同一个季度。 )As it turned out WOW had purposely disabled pretty much everything that allows doing action on a clock because it could break the game or enable bots.我想要重新创建一个时钟,一旦它被带走,我不得不做一些疯狂,像创建一个工作数组(一个动作和执行时间),然后注册一个事件处理程序上一堆常见的事件,像鼠标移动,然后在偶数处理程序中,处理任何时间到来的动作。事件处理程序实际上不会每X毫秒发生一次,但如果它每2-100毫秒发生一次,那就足够了。可悲的是我从来没有尝试过。

解决方法

它不会比这更容易。睡眠可能在您的FLTK或任何其他实现,但这涵盖了所有最好的方法做标准类别的系统睡眠没有特殊的事件中断。看哪:
-- we "pcall" (try/catch) the "ex",which had better include os.sleep
-- it may be a part of the standard library in future Lua versions (past 5.2)
local ok,ex = pcall(require,"ex")
if ok then
   -- print("Ex")
   -- we need a hack now too? ex.install(),you say? okay
   pcall(ex.install)
   -- let's try something else. why not?
   if ex.sleep and not os.sleep then os.sleep = ex.sleep end
end

if not os.sleep then
   -- we make os.sleep
   -- first by trying ffi,which is part of LuaJIT,which lets us write C code
   local ok,ffi = pcall(require,"ffi")
   if ok then
      -- print("FFI")
      -- we can use FFI
      -- let's just check one more time to make sure we still don't have os.sleep
      if not os.sleep then
         -- okay,here is our custom C sleep code:
         ffi.cdef[[
            void Sleep(int ms);
            int poll(struct pollfd *fds,unsigned long nfds,int timeout);
         ]]
         if ffi.os == "Windows" then
            os.sleep = function(sec)
               ffi.C.Sleep(sec*1000)
            end
         else
            os.sleep = function(sec)
               ffi.C.poll(nil,sec*1000)
            end
         end
      end
   else
      -- if we can't use FFI,we try LuaSocket,which is just called "socket"
      -- I'm 99.99999999% sure of that
      local ok,socket = pcall(require,"socket")
      -- ...but I'm not 100% sure of that
      if not ok then local ok,"luasocket") end
      -- so if we're really using socket...
      if ok then
         -- print("Socket")
         -- we might as well confirm there still is no os.sleep
         if not os.sleep then
            -- our custom socket.select to os.sleep code:
            os.sleep = function(sec)
               socket.select(nil,nil,sec)
            end
         end
      else
         -- now we're going to test "alien"
         local ok,alien = pcall(require,"alien")
         if ok then
         -- print("Alien")
         -- beam me up...
            if not os.sleep then
               -- if we still don't have os.sleep,that is
               -- now,I don't know what the hell the following code does
               if alien.platform == "windows" then
                  kernel32 = alien.load("kernel32.dll")
                  local slep = kernel32.Sleep
                  slep:types{ret="void",abi="stdcall","uint"}
                  os.sleep = function(sec)
                     slep(sec*1000)
                  end
               else
                  local pol = alien.default.poll
                  pol:types('struct','unsigned long','int')
                  os.sleep = function(sec)
                     pol(nil,sec*1000)
                  end
               end
            end
         elseif package.config:match("^") then
            -- print("busywait")
            -- if the computer is politically opposed to NIXon,we do the busywait
            -- and shake it all about
            os.sleep = function(sec)
               local timr = os.time()
               repeat until os.time() > timr + sec
            end
         else
            -- print("NIX")
            -- or we get NIXed
            os.sleep = function(sec)
               os.execute("sleep " .. sec)
            end
         end
      end
   end
end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读