BackgroundTaskPool combines ipc.map and ipc.workqueue to implement a very simple, pollable, way to run a set of arbitrary Lua functions as background tasks. You construct a BackgroundTaskPool with the size of the thread pool you would like to run your tasks on.
local BackgroundTaskPool = require 'ipc.BackgroundTaskPool'
local pool = BackgroundTaskPool(13) -- create a pool with 13 worker threads
Call addTask with a function and a set of arguments to add a task into the pool.
for i = 1,42 do
pool.addTask(function(i)
return math.sqrt(i)
end, i)
end
You can optionally poll for the completion of all tasks or just one task.
-- is the task with id 7 done?
if pool.isDone(7) then
print('the 7th is done!')
end
-- are all the tasks done?
if pool.isDone() then
print('all tasks are done!')
end
When you want the result for a task, call getResult with the task id. If the task function threw an error then the getResult call will throw that same error.
for i = 1,42 do
assert(pool.getResult(i) == math.sqrt(i))
end
Note that ipc.BackgroundTaskPool will throw an error when attempting to serialize closures/upvalues. However ipc.workqueue provides :writeup() for serializing closures/upvalues.