Skip to content

Core\Time::sleep

Waits for a Duration, such as 500ms or 2s, and then continues.

A wait of zero or less returns at once and does not throw an error. This is useful when you compute the time that is left until a deadline, and the deadline has already passed. While one program waits, the server can run other requests, so a wait does not slow down other users. A wait can be cancelled, for example because a deadline passed. Then the code stops at the sleep call.

In a test with a fixed clock, #[Test(at: ...)], a sleep does not wait. It ends at once, and the test clock moves forward by its length.

The examples below show a retry that waits longer after each failure, a wait of zero or less, and a pause between batches of messages.

Parameters

  • $dCore\Time\DurationHow long to wait; zero or negative returns at once.

Return value

Returns void. Nothing.

Changelog

VersionChange
0.0.5In 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.

Examples

Wait longer before each retry

<?nvs
// Shows how to wait before you try an action again, and wait longer after each failure.
class Upload
{
// This is a fake upload. It fails twice and works on the third try.
public static function send(int $attempt): bool
{
return $attempt >= 3;
}
}
// `Core\Time::sleep` waits for a `Duration`. `50ms` is 50 milliseconds.
// After each failure the wait doubles, so this waits 50ms and then 100ms.
Core\Time\Duration $wait = 50ms;
int $attempt = 1;
while (!Upload::send($attempt)) {
echo 'attempt ', $attempt, ' failed, waiting ', $wait->toString(), "\n";
Core\Time::sleep($wait);
$wait = $wait->multipliedBy(2);
$attempt = $attempt + 1;
}
echo 'attempt ', $attempt, " worked\n";
Output
attempt 1 failed, waiting 50ms
attempt 2 failed, waiting 100ms
attempt 3 worked

A wait of zero or less

<?nvs
// Shows that a wait of zero or less returns at once, and how to wait only for the time that is left.
// A zero or negative `Duration` does not wait and does not throw an error.
var $start = Core\Time::monotonic();
Core\Time::sleep(0s);
Core\Time::sleep((5s)->negated());
echo Core\Time::monotonic()->minus($start)->toMilliseconds() < 5000 ? 'returned at once' : 'waited', "\n";
// Each step should take at least 30 milliseconds. The loop sleeps for the time
// that is left after the work. This prints "at least 90 ms".
Core\Time\Duration $step = 30ms;
var $first = Core\Time::monotonic();
array<string> $tasks = ['read', 'check', 'save'];
foreach ($tasks as string $task) {
var $began = Core\Time::monotonic();
echo 'step: ', $task, "\n";
Core\Time::sleep($step->minus(Core\Time::monotonic()->minus($began)));
}
echo Core\Time::monotonic()->minus($first)->toMilliseconds() >= 90 ? 'at least 90 ms' : 'too fast', "\n";
Output
returned at once
step: read
step: check
step: save
at least 90 ms

A pause between batches

<?nvs
// Shows how to send messages in small batches with a pause between them, so a mail server is not overloaded.
array<string> $recipients = ['ana@example.com', 'ben@example.com', 'cleo@example.com', 'dev@example.com', 'eli@example.com'];
// `Core\Arr::chunk` splits the list into batches of two.
// After each batch except the last, the program waits 100 milliseconds.
array<array<string>> $batches = Core\Arr::chunk($recipients, 2);
int $number = 1;
foreach ($batches as array<string> $batch) {
echo 'batch ', $number, ': ', Core\Str::join($batch, ', '), "\n";
if ($number < Core\Arr::count($batches) as int) {
Core\Time::sleep(100ms);
}
$number = $number + 1;
}
Output
batch 1: ana@example.com, ben@example.com
batch 2: cleo@example.com, dev@example.com
batch 3: eli@example.com