Polling¶
Polling is a technique where a script repeatedly checks the status of a resource or condition at regular intervals, until a result is available or a timeout is reached.
In PHP, polling is common when waiting for asynchronous results: checking a queue, a database row, a file, or a socket. Short polling loops with a sleep() or usleep() call between iterations, while long polling holds the HTTP connection open until the server has a response.
stream_select() is the native PHP primitive for polling multiple streams simultaneously without busy-waiting. For process-level polling, pcntl_waitpid() with the WNOHANG flag avoids blocking.
Polling trades simplicity for efficiency: it is easy to implement but may waste CPU cycles or impose latency. Event-driven alternatives such as libevent, ReactPHP, or Swoole coroutines can replace polling when throughput matters.
Several PHP native functions include polling as a configuration: curl_multi_select(), inotify_read().
<?php
// Short polling: check a database flag every second
$timeout = 30;
$start = time();
while (time() - $start < $timeout) {
$row = $pdo->query('SELECT done FROM jobs WHERE id = 1')->fetch();
if ($row['done']) {
echo 'Job finished.';
break;
}
sleep(1);
}
// Stream polling with stream_select()
$read = [$socket];
$write = null;
$except = null;
$changed = stream_select($read, $write, $except, seconds: 5);
if ($changed > 0) {
$data = fread($read[0], 1024);
}
?>
See also stream_select and pcntl_waitpid.
Related : sleep, Stream, Socket, Event Driven, Blocking Wait, Real Time