Windows 中的 PHP CLI:处理 Ctrl-C 命令? [英] PHP CLI in Windows: Handling Ctrl-C commands?

查看:41
本文介绍了Windows 中的 PHP CLI:处理 Ctrl-C 命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在命令行中处理 PHP 中的 CTRL+C?Pcntl_* 函数在 Windows 中不起作用.

How can I handle CTRL+C in PHP on the command line? Pcntl_* functions do not work in Windows.

推荐答案

从 PHP 7.4 开始,现在可以通过使用 sapi_windows_set_ctrl_handler() 函数.

As of PHP 7.4, this is now possible by registering a handler callback with the sapi_windows_set_ctrl_handler() function.

这由 sapi_windows_generate_ctrl_event(),可用于将信号分派到与调用方连接到同一控制台的其他进程.

This is complemented by sapi_windows_generate_ctrl_event(), which can be used to dispatch signals to other processes attached to the same console as the caller.

在用户空间中只能处理 CTRL-C 和 CTRL-BREAK 事件,无法安全实施关闭/注销/关机事件,因为届时操作系统可能处于不可预测的部分关机状态处理程序函数被调用,因此此时执行的任何代码都有弊大于利的风险.

Only the CTRL-C and CTRL-BREAK events can be handled in user space, the close/log-off/shutdown events cannot be implented safely as the operating system will likely be in an unpredictable state of partial shutdown by the time the handler function is invoked, so there is a risk that any code executed at this point will do more harm than good.

您可以在 MSDN 上找到有关底层机制的更多信息:

You can find more information about the underlying mechanism on MSDN:

PHP API 几乎与底层 C API 相同,唯一显着的区别是 PHP 只允许注册单个回调,因此处理程序没有有意义的返回值,引擎只是标记事件作为处理.这是为了保持实现简单,因为一堆函数可以很容易地在用户空间中实现,同样如果你不想处理一个事件,你可以简单地调用 exit.

The PHP API is almost identical to the underlying C API, the only notable difference being that PHP only permits a single callback to be registered, and consequently the handler does does not have a meaningful return value, the engine simply marks the events as handled. This is in order to keep the implementation simple, as a stack of functions can easily be implemented in userland, and likewise if you don't want to handle an event you can simply call exit.

这篇关于Windows 中的 PHP CLI:处理 Ctrl-C 命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆