Reference · Dates
sleep
sleep(num) -> nullWhat it does
Waits that many milliseconds, then gives null. Other requests run meanwhile: the wait
does not hold the server. There is no maximum. In a scheduled job or a tick the wait
ends early when the server stops or the program is loaded again, so a loop that runs
for ever stops; a request waits its sleep out. It is an effect, like random() and
read_file.
Examples
let before = now()let r = sleep(5)print(r, now() - before >= 5)null trueEdge cases
- Zero is fine and returns at once; a float is allowed, cut to whole milliseconds.
- It takes milliseconds, not seconds as Python's
time.sleepdoes.quillo checkwarns of a number literal between 0 and 1, which waits less than a millisecond:sleep(0.2)giveswarning: `sleep` takes milliseconds: `sleep(0.2)` waits 0.2 ms; for 0.2 seconds write `sleep(200)`.sleep(0),sleep(1)and a variable are not warned of. - It returns
null, so it ends an expression-bodied function withnull; put it before the value you mean. - In a job or a tick a server stopping or a reload ends the job there with a 503, as a
replywould, instead of letting it run on old code. - A request's sleep of 5 ms or more waits on a timer and holds no thread, so a hundred streams sleeping in their loops hold up no other request; a shorter one waits on a thread, for its accuracy (a timer's are whole milliseconds).
- In a
parallelfunction, a sleep ends at once when another of the functions fails.
print(sleep(0), sleep(1.5))fn wait_then(v) { sleep(1) v}print(wait_then("done"))null nulldoneErrors
A negative number, or anything that is not a number, is a runtime error, not an error value.
print(sleep(-1))line 1: `sleep` expects milliseconds (a number >= 0), got intprint(sleep("5"))line 1: `sleep` expects milliseconds (a number >= 0), got strChaining
It is rarely piped; in a loop it paces work, and in a tick it is what makes the tick slow:
let t = now()for i in range(0, 3) { sleep(2) }print(now() - t >= 6)trueschedule("every 1m", () => { for job in pending() { run(job) sleep(200) }})See also
now, schedule, every, parallel
Thread 0 posts
On sleep(): questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.
Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo