-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfutures.lua
More file actions
53 lines (43 loc) · 1.22 KB
/
futures.lua
File metadata and controls
53 lines (43 loc) · 1.22 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
local asyncio = require "lua-asyncio"
local async = asyncio.async
local loop = asyncio.loops.EventLoop.new()
local future = loop:new_object(asyncio.futures.Future)
-- you might change the intervals depending on what your os.time() returns
local long_task_1 = async(function()
loop:sleep(2)
print("Long task 1 finished")
return "something", "else", "with", "many", "arguments"
end)
local long_task_2 = async(function()
loop:sleep(7)
print("Long task 3 finished")
end)
local long_task_3 = async(function()
print("Start waiting")
local semaphore = loop:await_many(future, long_task_1(), long_task_2())
local result = loop:await(semaphore)
print("Future result", table.unpack(result[1]))
print("Task 2 result", table.unpack(result[2]))
print("Task 3 result", table.unpack(result[3]))
print("End")
end)
loop:add_task(long_task_3())
loop:add_task(asyncio.Task.new(function()
loop:sleep(5)
future:set_result({true, 56})
-- It is required to feed set_result with a table.
print("Set future result.")
end))
while true do
loop:run()
end
--[[ OUTPUT OF THE PROGRAM:
Start waiting
Long task 1 finished
Set future result.
Long task 3 finished
Future result true 56
Task 2 result something else with many arguments
Task 3 result
End
]]