Update async.lua (#3)

This commit is contained in:
userMacieG
2020-07-20 16:27:01 +02:00
committed by GitHub
parent 6db3f2ef7d
commit 8c9300cb11
+5 -22
View File
@@ -5,21 +5,17 @@ end
Async = {}
function Async.parallel(tasks, cb)
if #tasks == 0 then
cb({})
return
end
local remaining = #tasks
local results = {}
local results = {}
for i=1, #tasks, 1 do
for i = 1, #tasks, 1 do
CreateThread(function()
tasks[i](function(result)
table.insert(results, result)
remaining = remaining - 1;
@@ -27,64 +23,51 @@ function Async.parallel(tasks, cb)
if remaining == 0 then
cb(results)
end
end)
end)
end
end
function Async.parallelLimit(tasks, limit, cb)
if #tasks == 0 then
cb({})
return
end
local remaining = #tasks
local running = 0
local queue = {}
local results = {}
local running = 0
local queue, results = {}, {}
for i=1, #tasks, 1 do
table.insert(queue, tasks[i])
end
local function processQueue()
if #queue == 0 then
return
end
while running < limit and #queue > 0 do
local task = table.remove(queue, 1)
running = running + 1
task(function(result)
table.insert(results, result)
remaining = remaining - 1;
running = running - 1
running = running - 1
if remaining == 0 then
cb(results)
end
end)
end
CreateThread(processQueue)
end
processQueue()
end
function Async.series(tasks, cb)