Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

How React PHP handles async non-blocking 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 ?

like image 543
Reza Shadman Avatar asked Jun 16 '15 09:06

Reza Shadman


People also ask

Is PHP blocking or nonblocking?

So, yeah, PHP is blocking, so using a timeout with a function was out of the question. There are a number of workarounds, but they're not very robust. But then I remembered Amp. Amp (and ReactPHP) are frameworks for asynchronous programming in PHP.

Does PHP support asynchronous?

No. As detailed in the previous section, we need a scheduler (or event loop) in order to run things asynchronously or concurrently. This means you would still have to use something like ReactPHP, Swoole or Amp for async PHP. With or without fibers, async PHP will be provided by external libraries.

Is PHP synchronous or async?

PHP serves requests synchronously. It means that each line of code executes in the synchronous manner of the script. After getting the result from one line it executes next line or wait for the result before jumping to the execution of the next line of code.

Is Async same as non-blocking?

A nonblocking call returns immediately with whatever data are available: the full number of bytes requested, fewer, or none at all. An asynchronous call requests a transfer that will be performed in its whole(entirety) but will complete at some future time.


1 Answers

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)

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)

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; 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.

like image 158
Method Avatar answered Oct 16 '22 19:10

Method