首页 > 解决方案 > 我可以让我的 Lua 程序睡一天左右吗?

问题描述

我想让我的 mydicebot (by seuntje) Lua 程序在一天内睡觉,在赌了一天之后......就像

function sleep(n)
  t = os.clock()
  while os.clock() - t <= n do
    -- nothing
  end
end

function playsleep() 
  sec = math.random(80000,90000) 
  sleep(sec)     -- around 86400 seconds
end

timestart = os.time() 
dur = math.random(70000,80000)


function dobet() 
   if os.time() - timestart < math.random then
      playsleep()
   end
   timestart = os.time()     -- reset the time counter
end

但是当我在 dobet 函数中调用 playsleep 函数时

  1. 最终我无法单击程序中的任何内容,也无法移动另一个选项卡
  2. 而且 CPU 也不在休眠,甚至变得很忙
  3. 有时甚至在 90000 秒后仍然卡住

-- 问题 --

A. 那么我可以制作一个睡眠是真正睡眠的功能吗?

B. 能睡到 90000 秒吗?

C. 或者上面的变量“sec”的最大睡眠时间是多少?

标签: luasleepdurationseconds

解决方案


使用 posix 模块休眠:

posix = require("posix")
posix.sleep(86400)

但这仍然会阻止您的程序,您将无法单击任何内容。您需要提供有关您的计划的更多详细信息,以便获得更好的建议。


推荐阅读