-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtimeout.lua
More file actions
40 lines (32 loc) · 706 Bytes
/
timeout.lua
File metadata and controls
40 lines (32 loc) · 706 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
local asyncio = require "lua-asyncio"
local async = asyncio.async
local loop = asyncio.loops.EventLoop.new()
-- you might change the intervals depending on what your os.time() returns
local eternity = async(function()
print("Start eternity")
loop:sleep(3600)
print("End eternity")
end)
local long_task = async(function()
print("Start waiting")
local task = eternity()
loop:await_for(task, 1)
if task.cancelled then
print("Task cancelled.")
elseif task.error then
error(task.error)
else
print("Task successfully finished.")
end
print("End")
end)
loop:add_task(long_task())
while true do
loop:run()
end
--[[ OUTPUT OF THE PROGRAM:
Start waiting
Start eternity
Task cancelled.
End
]]