如何让我的Lua程序在大约一天后休眠?

我想让我的mydicebot(由seuntje制作)Lua程序在下注一天后休眠大约一天...像这样

function sleep(n)
  t = os.clock()
  while os.clock() - t <= n do
    -- 什么也不做
  end
end

function playsleep()
  sec = math.random(80000,90000)
  sleep(sec)     -- 大约86400秒
end

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

function dobet()
   if os.time() - timestart < math.random then
      playsleep()
   end
   timestart = os.time()     -- 重置时间
end

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

  1. 我无法单击程序中的任何内容,也无法移动其他选项卡
  2. CPU也没有休眠,甚至在忙碌
  3. 有时候在90000秒后甚至会卡住

-- 问题 --

A. 如何让休眠函数真正休眠?

B. 它可以休眠多达90000秒吗?

C.或者上述变量“sec”的最大休眠秒数是多少?

点赞
用户67022
用户67022

使用 posix 模块进行睡眠:

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

但是这仍然会阻塞您的程序,您将无法点击任何东西。您需要提供有关程序的更多详细信息,以便获得更好的建议。

2021-06-22 02:19:57
用户11740758
用户11740758

也可以使用 os

为什么不试试下面的代码:

do os.execute('$(type -path sleep) '..(3600*24)) end
2021-06-22 07:24:27