-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer-with-signal.lua
More file actions
37 lines (31 loc) · 991 Bytes
/
timer-with-signal.lua
File metadata and controls
37 lines (31 loc) · 991 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
-- Time demo with signal handling fpr CTRL-C
local uv = require "luv"
local lsleep = require "lsleep"
local loopCycleMillis <const> = 1000
-- Create a handle to a uv_timer_t
local timer = uv.new_timer()
timer:set_repeat(loopCycleMillis)
-- Create a new signal handler
local signal = uv.new_signal()
-- Define a handler function
signal:start("sigint",
function(signal)
-- print("got " .. signal .. ", shutting down")
print("\nCTRL-C pressed. Shutting down.")
timer:close()
uv.stop()
os.exit(1)
end
)
-- Callback function
function doStuff()
local timestamp = os.date("%Y-%m-%d_%H:%M:%S")
io.write(string.format("Tick! %s. Next due in: %dms.\n",timestamp,timer:get_due_in()))
-- lsleep:sleep(5)
timer:again()
end
-- This will wait loopCycleMillis ms and then continue inside the callback
timer:start(0, loopCycleMillis, doStuff)
-- uv.run will block and wait for all events to run.
-- When there are no longer any active handles, it will return
uv.run()