跳到主要内容

服务调度

Lua协程

---创建一个新的协程并立即开始执行, 带有`async`标记的函数都需要在`moon.async`中调用。如果 fn 函数没有调用 coroutine.yield, 则会同步执行。
--- ```lua
--- local function foo(a, b)
--- print("start foo", a, b)
--- moon.sleep(1000)
--- print("end foo", a, b)
--- end
--- local function bar(a, b)
--- print("start bar", a, b)
--- moon.sleep(500)
--- print("end bar", a, b)
--- end
--- moon.async(foo, 1, 2)
--- moon.async(bar, 3, 4)
--- ```
---
---@param fn fun(...) @需要异步执行的函数
---@param ... any @可选参数,传递给 fn 函数
---@return thread @新创建的协程
moon.async(fn, ...)

定时器

创建协程风格的定时器moon.sleep

    ---阻塞当前协程至少`mills`毫秒
---@async
---@param mills integer@ 毫秒
---@return boolean @ `moon.wakeup`唤醒的定时器返回`false`, 正常触发的定时器返回`true`
moon.sleep(mills)

示例

    moon.async(function()
print("coroutine timer start")
moon.sleep(1000)
print("coroutine timer tick 1 seconds")
moon.sleep(1000)
print("coroutine timer tick 1 seconds")
moon.sleep(1000)
print("coroutine timer tick 1 seconds")
moon.sleep(1000)
print("coroutine timer tick 1 seconds")
moon.sleep(1000)
print("coroutine timer tick 1 seconds")
print("coroutine timer end")
end)
    2023-06-16 20:37:24.824 | :01000001 | INFO  | coroutine timer start     (example_timer.lua:9)
2023-06-16 20:37:25.830 | :01000001 | INFO | coroutine timer tick 1 seconds (example_timer.lua:11)
2023-06-16 20:37:26.841 | :01000001 | INFO | coroutine timer tick 1 seconds (example_timer.lua:13)
2023-06-16 20:37:27.850 | :01000001 | INFO | coroutine timer tick 1 seconds (example_timer.lua:15)
2023-06-16 20:37:28.857 | :01000001 | INFO | coroutine timer tick 1 seconds (example_timer.lua:17)
2023-06-16 20:37:29.869 | :01000001 | INFO | coroutine timer tick 1 seconds (example_timer.lua:19)
2023-06-16 20:37:29.869 | :01000001 | INFO | coroutine timer end (example_timer.lua:20)

使用moon.sleep(0)让出协程执行权

moon.async创建的lua协程占用执行权后,其他的协程需要等待,可以使用moon.sleep(0)让出当前协程执行权

    ---模拟繁重的任务
local function heavy_task(name)
local i = 0
print(name, "begin task")
while (i < 200000000) do
i = i + 1
if i % 50000000 == 0 then
moon.sleep(0)
print(name, "task yield")
end
end
print(name, "end task", i)
end

moon.async(heavy_task, "task1")
moon.async(heavy_task, "task2")
    2023-06-16 20:30:02.440 | :01000001 | INFO  | task1     begin task      (example_timer.lua:35)
2023-06-16 20:30:04.679 | :01000001 | INFO | task2 begin task (example_timer.lua:35)
2023-06-16 20:30:06.910 | :01000001 | INFO | task1 task yield (example_timer.lua:40)
2023-06-16 20:30:09.129 | :01000001 | INFO | task2 task yield (example_timer.lua:40)
2023-06-16 20:30:11.375 | :01000001 | INFO | task1 task yield (example_timer.lua:40)
2023-06-16 20:30:13.593 | :01000001 | INFO | task2 task yield (example_timer.lua:40)
2023-06-16 20:30:15.822 | :01000001 | INFO | task1 task yield (example_timer.lua:40)
2023-06-16 20:30:18.086 | :01000001 | INFO | task2 task yield (example_timer.lua:40)
2023-06-16 20:30:20.349 | :01000001 | INFO | task1 task yield (example_timer.lua:40)
2023-06-16 20:30:20.349 | :01000001 | INFO | task1 end task 200000000 (example_timer.lua:43)
2023-06-16 20:30:20.349 | :01000001 | INFO | task2 task yield (example_timer.lua:40)
2023-06-16 20:30:20.349 | :01000001 | INFO | task2 end task 200000000 (example_timer.lua:43)

使用moon.wakeup唤醒moon.sleep阻塞的协程

    local co = moon.async(function()
print("wakeup", moon.sleep(10000))
end)

moon.async(function()
print("normal", moon.sleep(1000))
moon.wakeup(co)
end)
    2023-06-16 20:36:44.494 | :01000001 | INFO  | normal    true    (example_timer.lua:28)
2023-06-16 20:36:44.494 | :01000001 | INFO | wakeup false (example_timer.lua:24)

创建回调方式的定时moon.timeout

    ---创建一个定时器,等待的mills毫秒后触发回调函数。如果`mills<=0`则这个函数的行为退化成向消息队列post一条消息,对于需要延迟(delay)执行的操作非常有用。
---@param mills integer @等待的毫秒数
---@param fn fun() @调用的函数
---@return integer @ 返回timerid,可以使用`moon.remove_timer`删除定时器
moon.timeout(mills, fn)

示例

    local timerid = moon.timeout(1000, function()
print("hello world")
end)

删除定时器moon.remove_timer

    local timerid = moon.timeout(1000, function()
error("must not print")
end)
moon.remove_timer(timerid)

获取时间

获取当前毫秒时间戳moon.now(),可能会有误差

    moon.async(function()
print("start", moon.now())
moon.sleep(1000)
print("1 seconds later", moon.now())
moon.sleep(2000)
print("2 seconds later", moon.now())
print("end", moon.now())
end)
    2023-06-16 20:48:28.664 | :01000001 | INFO  | start     1686919708655   (example_timer.lua:51)
2023-06-16 20:48:29.666 | :01000001 | INFO | 1 seconds later 1686919709666 (example_timer.lua:53)
2023-06-16 20:48:31.672 | :01000001 | INFO | 2 seconds later 1686919711672 (example_timer.lua:55)
2023-06-16 20:48:31.672 | :01000001 | INFO | end 1686919711672 (example_timer.lua:56)

获取当前秒时间戳moon.time()

    moon.async(function()
print("start", moon.time())
moon.sleep(1000)
print("1 seconds later", moon.time())
moon.sleep(2000)
print("2 seconds later", moon.time())
print("end", moon.time())
end)
    2023-06-16 20:49:16.434 | :01000001 | INFO  | start     1686919756      (example_timer.lua:51)
2023-06-16 20:49:17.432 | :01000001 | INFO | 1 seconds later 1686919757 (example_timer.lua:53)
2023-06-16 20:49:19.441 | :01000001 | INFO | 2 seconds later 1686919759 (example_timer.lua:55)
2023-06-16 20:49:19.441 | :01000001 | INFO | end 1686919759 (example_timer.lua:56)

使用moon.clock()获取函数执行时间

    moon.async(function()
local t1 = moon.clock()
print("start", t1)
moon.sleep(1235)
local t2 = moon.clock()
print("end", t2)
print("cost", t2 - t1)
end)
    2023-06-16 20:52:13.310 | :01000001 | INFO  | start     0.039369        (example_timer.lua:61)
2023-06-16 20:52:14.543 | :01000001 | INFO | end 1.2728892 (example_timer.lua:64)
2023-06-16 20:52:14.543 | :01000001 | INFO | cost 1.2335202 (example_timer.lua:65)