大网数据广告活动
PHP 语言与运行手册

pcntl_async_signals · Enable/disable asynchronous signal handling or return the old setting

· 52CE 我爱测

pcntl_async_signals — Enable/disable asynchronous signal handling or return the old setting

pcntl_async_signals

(PHP 7 >= 7.1.0, PHP 8)

pcntl_async_signals — Enable/disable asynchronous signal handling or return the old setting

Description

function pcntl_async_signals(?bool $enable = null): bool

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。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
PHP Documentation Group
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。