Skip to content

Commit d199a53

Browse files
committed
Improve scheduler examples
1 parent a5d7427 commit d199a53

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

docs/Runtime Environment/Scheduler.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ The `run` method activates the scheduler. The scheduler resumes all coroutines a
1515
```pluto
1616
local scheduler = new (require"pluto:scheduler") ()
1717
18+
-- Make this example behave a bit better in the non-blocking WASM environment
19+
if coroutine.isyieldable() then
20+
scheduler.yieldfunc = || -> coroutine.sleep(1)
21+
end
22+
1823
local loops = 0
1924
scheduler:addloop(function()
2025
++loops
@@ -50,8 +55,8 @@ scheduler:run()
5055
```pluto
5156
local scheduler = new (require"pluto:scheduler") ()
5257
53-
-- Yield to OS every second instead of every millisecond.
54-
scheduler.yieldfunc = || -> os.sleep(1000)
58+
-- Use coroutine.sleep instead of os.sleep in a non-blocking environment, and increase the timeout from 1 to 1000 ms (1 second).
59+
scheduler.yieldfunc = || -> (coroutine.isyieldable() ? coroutine : os).sleep(1000)
5560
5661
local loops = 0
5762
scheduler:addloop(function()

0 commit comments

Comments
 (0)