pcntl_async_signals
(PHP 7 >= 7.1.0, PHP 8)
pcntl_async_signals — Enable/disable asynchronous signal handling or return the old setting
Description
If the enable parameter is null,
pcntl_async_signals() returns whether asynchronous signal
handling is enabled. Otherwise, asynchronous signal handling is enabled or
disabled.
Parameters
enable
Whether asynchronous signal handling should be enabled.
Return Values
When used as getter (enable parameter is null) it returns
whether asynchronous signal handling is enabled. When used as setter (enable
parameter is not null), it returns whether asynchronous signal
handling was enabled before the function call.
Changelog
| Version | Description |
|---|---|
| 8.0.0 |
enable is nullable now.
|
Examples
Example #1 Calling signal handlers during a cURL transfer
Without the progress callback, the handler would not be called until the
transfer has completed. CURLOPT_XFERINFOFUNCTION is
available as of PHP 8.2.0; use CURLOPT_PROGRESSFUNCTION
on earlier versions.
<?php
pcntl_async_signals(true);
pcntl_signal(SIGALRM, function () {
echo "Heartbeat\n";
pcntl_alarm(1);
});
pcntl_alarm(1);
$ch = curl_init('https://example.com/slow-endpoint');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Calling back into PHP code during the transfer lets the handler run
curl_setopt($ch, CURLOPT_NOPROGRESS, false);
curl_setopt($ch, CURLOPT_XFERINFOFUNCTION, function () {
return 0;
});
$response = curl_exec($ch);
?>Notes
With asynchronous signal handling enabled, a handler installed with pcntl_signal() is not called at the moment the signal is received. The signal is recorded, and the handler is called the next time the engine checks for pending signals while executing PHP code, for instance after a function call returns, or on the next iteration of a loop.
As a consequence, a long-running call to an internal function, such as
curl_exec() or a database query, is not interrupted to
call the handler: the handler is only called once that function returns.
The handler is called earlier only if the function calls back into PHP code
while it runs, for instance through a
CURLOPT_XFERINFOFUNCTION callback.
A signal may still interrupt a system call the function is blocked in. For instance, sleep() then returns early, and the handler is called once it has returned.
See Also
来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。
