Function
flock
Locks a file, letting other coroutines run while it waits for the lock.
Description#
function flock(
$stream,
int $operation,
?int &$would_block = null
): boolInside a coroutine, a blocking lock request is retried with LOCK_NB and a phasync::yield()
between the attempts, until it is granted or fails for another reason than the file being locked.
With LOCK_NB, and outside a coroutine, it is the native flock().
Parameters#
- $stream
- A stream resource
- $operation int
LOCK_SH,LOCK_EXorLOCK_UN, optionally withLOCK_NB- $would_block ?int = null
- Set by the native
flock()whenLOCK_NBis given, or outside a coroutine; left alone otherwise
Return values#
bool
Errors / Exceptions#
| Exception | When |
|---|---|
TypeError | When $stream is not a stream resource |
Examples#
use function phasync\flock;
$fp = fopen('/tmp/job.lock', 'c');
flock($fp, LOCK_EX); // other coroutines run while another process holds the lockSee also#
file_put_contentsWrites a file, letting other coroutines run while it waits.