sapi_windows_generate_ctrl_event

(PHP 7 >= 7.4.0, PHP 8)

sapi_windows_generate_ctrl_eventCTRLイベント を別プロセスに送信する

説明

sapi_windows_generate_ctrl_event(int $event, int $pid = 0): bool

同じプロセスグループの別プロセスに CTRLイベント を送信する。

パラメータ

event

送信する CTRL イベント: PHP_WINDOWS_EVENT_CTRL_C または PHP_WINDOWS_EVENT_CTRL_BREAK

pid

イベントを送信するプロセスID。 0 に設定すると、プロセスグループに属する 全てのプロセスにイベントが送信されます。

戻り値

成功した場合に true を、失敗した場合に false を返します。

例1 sapi_windows_generate_ctrl_event() 関数の基本的な使い方

以下の例は、 CTRL+BREAK イベントを子プロセスに送信する方法を示しています。 この場合、ユーザーが CTRL+BREAK を押すまで、 子プロセスは I'm still alive を1秒ごとに表示します。 CTRL+BREAK は、子プロセスのみを終了させます。

<?php
// CTRL+BREAK イベントを子プロセスに送信する
sapi_windows_set_ctrl_handler('sapi_windows_generate_ctrl_event');

// 1秒ごとに echo する子プロセスを生成
$cmd = ['php', '-r', 'while (true) { echo "I\'m still alive\n"; sleep(1); }'];
$descspec = array(['pipe', 'r'], ['pipe', 'w'], ['pipe', 'w']);
$options = ['create_process_group' => true];
$proc = proc_open($cmd, $descspec, $pipes, null, null, $options);
while (
true) {
echo
fgets($pipes[1]);
}
?>

参考

add a note

User Contributed Notes 1 note

up
1
Michael Q
3 years ago
This function may produce a Warning:

sapi_windows_set_ctrl_handler(): CTRL events trapping is only supported on console in script.php..

It happens when script is started by "php-cgi.exe", so "php.exe -q" should be used instead.

While pressing CTRL+C don't expect the handler to run instantly, i've ran some curl request in a loop and found that handler runs when either response arrives or request finishes by timeout.
To Top