React PHP 如何处理异步非阻塞 I/O?

How React PHP handles async non-blocking I/O?

React PHP 如何处理异步非阻塞 I/O ?

Nodejs 使用其事件队列处理不同线程上的 I/O。它为此使用 libuv。正如 PHP 中没有那样的东西,React 如何处理单线程上的非阻塞 I/O 进程?

React PHP 提供应用程序的主要事件循环;您仍然需要以非阻塞方式编写代码,因为它都在一个线程上。可能的解决方案都是围绕 php 的使用,这与我相信大多数 php 开发人员习惯的不同……尽管 React PHP 提供了主循环;大部分 React PHP 库是 sockets/streams/promise/etc 的实现。这些都采用了方法来实现对I/O的非阻塞访问;通常通过使用 stream_set_blocking (http://php.net/manual/en/function.stream-set-blocking.php)

其他选项包括编程类似于 FSM 的东西 (https://en.wikipedia.org/wiki/Finite-state_machine); which allows it to continously update it's current state as it progresses; each time allowing for certain chunks of code to run, then giving up the thread to anything else in the loop. Essentially implementing your own time-slicing (https://en.wikipedia.org/wiki/Preemption_(computing)#Time_slice)

另一种选择是实现线程(http://php.net/manual/en/book.pthreads.php) which is not enabled by default usually; And the last option I can think of is using process control to either fork/start/control other processes (http://php.net/manual/en/intro.pcntl.php),它只在 *nix 系统上启用;让您的主机 CPU 控制时间片;您只需要将您的应用程序架构为线程安全、与消息队列通信或其他某种机制。

tldr;使用您的应用程序架构不导致 php 阻塞,将您的流设置为不阻塞,或使用 thread/process 控制来管理您自己的多线程。