React PHP如何处理异步非阻塞I/O? [英] How React PHP handles async non-blocking I/O?

查看:90
本文介绍了React PHP如何处理异步非阻塞I/O?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

Nodejs使用其事件队列来处理不同线程上的I/O.它为此使用libuv.像在PHP中一样,React如何在单个线程上处理非阻塞I/O进程?

Nodejs uses its event queue which handles I/O on different threads. It uses libuv for this. As in PHP there isn't something like that, How React handles non-blocking I/O process on a single thread ?

推荐答案

React PHP提供了应用程序的主要事件循环.您仍然需要以非阻塞的方式编写代码,因为它们全部在一个线程上.所有可能的解决方案都围绕使用php而不是我确定大多数php开发人员习惯于使用php进行.React PHP库的大部分是socket/streams/promise/etc的实现.这些都采用了实现对I/O的非阻塞访问的方法.通常通过使用 stream_set_blocking ( http://php.net/manual/zh-CN/function.stream-set-blocking.php )

React PHP provides the primary event loop of the application; you are still required to write your code in a non-blocking way since it is all on one thread. The possible solutions to this all revolve around using php differently than I am sure most php developers are used to... Although React PHP provides the main loop; the bulk of the React PHP libraries are the implementations for sockets/streams/promise/etc. These have all employed methods to achieve non-blocking access to the I/O; typically through the use of stream_set_blocking (http://php.net/manual/en/function.stream-set-blocking.php)

其他选项包括对类似于FSM的程序进行编程( https://en.wikipedia.org/wiki/Finite-state_machine );允许它随着进度不断更新其当前状态;每次允许运行某些代码块,然后将线程放弃到循环中的其他任何线程.本质上实现自己的时间切片( https://en.wikipedia.org/wiki/Preemption_(computing)#Time_slice )

The other options include programming something similar to a 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 ),通常默认情况下未启用;我能想到的最后一个选择是使用流程控制来派生/启动/控制其他流程( http://php.net/manual/en/intro.pcntl.php ),仅在* nix系统上启用;这可以让您的主机CPU控制时间分片;您只需要将应用程序架构为线程安全的,与消息传递队列进行通信或其他某种机制即可.

Another option is to implement threads (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) which is only enabled on *nix systems; which lets your host CPU control the time slicing; you will just be required to architect your application to either be thread safe, communicate with messaging queues, or some other mechanism.

tldr;使用您的应用程序体系结构不会导致php阻塞,不要将流设置为不阻塞,或者使用线程/进程控制来管理自己的多线程.

tldr; Use your application architecture to not cause php to block, set your streams not to block, or use thread/process control to manage your own multi-threading.

这篇关于React PHP如何处理异步非阻塞I/O?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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