Core\Task::all
Core\Task::all(
{ name: callable(): T, } ...$tasks,
{limit?: uint, deadline?: Core\Time\Duration},
): SCore\Task::all() runs several callables (functions with no parameters) at the same time and waits until all finish. You give them as the fields of an anonymous object, such as {user: fn() => ..., orders: fn() => ...}. The result has the same field names, and each field has its callable's return type.
If one callable throws an error, the others stop, and the call throws that error. The option limit sets how many callables run at once. The option deadline is a time limit for the whole call. When it runs out, the call throws a TimeoutError.
Each callable runs as a task. A task has the same capabilities (rights, such as reading files) and the same memory limit as the code that calls all(). A task that needs more memory than the limit stops the program with an error.
Good to know: the deadline is checked only while a callable waits, such as for a web service. The cpu_time limit in nvs.toml stops an endless loop. In a test with #[Test(at: ...)], the deadline uses the test's clock.
Parameters
...$tasks{ name: callable(): T, }variadicAn object whose fields are anonymous functions with no parameters, such as{ a: fn (): int => 1 }. Write them directly in the call. A variable of typecallablein a field does not compile.
Options
limituintoptionalThe most tasks that run at the same time. The other tasks wait until one finishes. Without it, every task starts at once.deadlineCore\Time\DurationoptionalThe time limit for the whole call, not for each task. Without it, the request's[limits] wall_timesetting is the limit.
Return value
Returns S. An object with the value each function returned. No task is still running when the call returns. If a task throws an error, the other tasks are stopped, and the call throws that same error.
Errors
LogicErrorWhenlimitis0. No task could start, so the call could never finish.TimeoutErrorWhendeadlineruns out before every task has finished. Every task is stopped first, and the call waits until they have stopped.
Changelog
| Version | Change |
|---|---|
0.0.5 | In a test with #[Test(at:)], Core\Time::sleep ends at once and moves the test clock forward. A timeout of Core\Task::all and the time to live of a Core\Cache::memory entry use the same clock. Without at:, a sleep waits in real time. |
0.0.6 | Each task that Core\Task::all runs has the same capabilities and the same memory limit as the code that calls it. A task that needs more memory than the limit does not get it. |
Examples
Load two things at once
<?nvs// Shows how to run two pieces of work at the same time and use both results.
// Each field of the object is an anonymous function. `Core\Task::all` runs them// at the same time and waits until both have finished.var $page = Core\Task::all({ name: fn(): string => Core\Str::upper('anna'), orders: fn(): array<int> => [1042, 1043, 1051],});
// The result has the same field names. `$page->name` is a `string` and// `$page->orders` is an `array<int>`. This prints `ANNA has 3 orders.`echo $page->name, ' has ', Core\Arr::count($page->orders), " orders.\n";ANNA has 3 orders.One task throws an error
<?nvs// Shows what happens when one of the tasks throws an error.
try { var $result = Core\Task::all({ price: fn(): int => 1999, stock: fn(): int => { throw new RuntimeError('the stock service is down'); }, }); echo 'price: ', $result->price, "\n";} catch (RuntimeError $e) { // The first error stops the other tasks, and `Core\Task::all` throws it. // This prints `failed: the stock service is down`. echo 'failed: ', $e->message, "\n";}failed: the stock service is downA dashboard with a time limit
<?nvs// Shows how to load a dashboard from slow services with a time limit.
class Dashboard{ public static function load(Core\Time\Duration $limit): string { try { // `limit: 2` runs at most two tasks at the same time. // `deadline` is the time limit for all three tasks together. var $data = Core\Task::all({ visits: fn(): int => { Core\Time::sleep(20ms); return 5120; }, signups: fn(): int => { Core\Time::sleep(20ms); return 48; }, revenue: fn(): int => { Core\Time::sleep(20ms); return 73100; }, }, { limit: 2, deadline: $limit }); return 'visits ' . $data->visits . ', signups ' . $data->signups . ', revenue ' . $data->revenue; } catch (TimeoutError $e) { // When the time is over, every task is stopped and // `Core\Task::all` throws a `TimeoutError`. return 'the dashboard is not available right now'; } }}
// Two seconds is enough time, so this prints the numbers.echo Dashboard::load(2s), "\n";// Five milliseconds is not enough, so this prints the error text.echo Dashboard::load(5ms), "\n";visits 5120, signups 48, revenue 73100the dashboard is not available right now