Concurrent
libraryRuns several Lua functions at the same time on worker threads and waits until all of them are done. The functions share the global variables of the script and nothing is synchronised, so keep them independent and let each write to its own variable or table slot. Browser sessions and other library objects are not made thread-safe by this library.
- Kind Library, a global in every script
- Category Utilities
- Functions 1
Functions
-
runboolean Starts a pool of `threads` worker threads, runs every function of the table on it and blocks until all have finished.
Concurrent.run(threads: number, functions: table)→boolean Starts a pool of `threads` worker threads, runs every function of the table on it and blocks until all have finished. Functions are called without arguments, their results are discarded, and entries of the table that are not functions are ignored. With fewer threads than functions the rest waits for a free thread. A function that fails does not stop the others and does not raise an error here: its error is collected instead. The call returns two values, a boolean that is true when nothing failed and a list with one text per failure. That text is the plain error message, such as `script.lua:3: message`, without Java class names. The order of the list is the order in which the failures happened. Throws when `threads` is not a whole number (`First parameter must be a integer`), when it is below 1, or when the second argument is not a table.
Parameters
-
threadsnumber - Number of worker threads, a whole number of at least 1
-
functionstable - List of Lua functions to run, like `{ fnA, fnB }`
Returns
Example runs in CI
local ok, errors = Concurrent.run(2, { function() print("first") end, function() print("second") end,})print(ok, #errors)