Method
Claim::acquire
Take the name, trying every 20 ms for up to $timeout seconds when another holds it.
Description#
public Claim::acquire(
float $timeout = 0.0
): ?staticWhoever tries first when the name frees gets it, in no order. Returns this handle when it holds the name, also if it did already; null when it could not.
Parameters#
- $timeout float = 0.0
- Seconds to keep trying while another holds the name; 0 tries once
Return values#
This handle, or null when the name is held by another ?static
Examples#
use Swerve\Swerve;
phasync::run(function () {
phasync::go(function () {
$claim = Swerve::claim('import')->acquire();
echo "first: importing\n";
phasync::sleep(0.2);
echo "first: done\n";
}); // released as $claim goes
phasync::go(function () {
echo "second: waiting\n";
if ($claim = Swerve::claim('import')->acquire(timeout: 5.0)) { // tries every 20 ms
echo "second: importing\n";
}
});
});Output
first: importing second: waiting first: done second: importing
if ($leader = Swerve::claim('leader')->acquire()) {
// this worker holds the name until $leader is released or destroyed
}See also#
Claim::releaseGive the claim up, so that another can take it.Claim::availableWhether nobody holds the name now; nothing is claimed.