How does React PHP handle asynchronous non-blocking I / O?

How does React PHP handle asynchronous non-blocking I / O?

Nodejs uses an event queue that handles I / O on different threads. He uses libuv for this. Like PHP, there is nothing like this: How does React handle a non-blocking I / O process in a single thread?

+6
source share
1 answer

React PHP provides the main application event loop; you still need to write your code in a non-blocking way, as it is on the same thread. Possible solutions to all of this relate to using php differently than I'm sure most php developers are using ... Although React PHP provides a basic loop; The bulk of React PHP libraries is an implementation for sockets / streams / promises / etc. They use all the methods used to achieve non-blocking I / O access; usually using stream_set_blocking ( http://php.net/manual/en/function.stream-set-blocking.php )

Other options include programming something similar to FSM ( https://en.wikipedia.org/wiki/Finite-state_machine ); which allows him to constantly update the current state as he progresses; each time allowing you to run certain pieces of code, and then discarding the thread from anything else in the loop. Essentially implementing its own time reference ( https://en.wikipedia.org/wiki/Preemption_(computing)#Time_slice )

Another option is to implement threads ( http://php.net/manual/en/book.pthreads.php ), which are usually not included by default; And the last option that I can think of is to use process control or fork / start / control of other processes ( http://php.net/manual/en/intro.pcntl.php ), which is enabled only on * nix systems ; which allows your host processor to control the time slice; you just need to archive the application to either be thread safe, or communicate with messaging queues, or some other mechanism.

TL; DR; Use your application architecture to not force php to block, set threads to not block, or use thread / process control to control your own multi-threaded processing.

+12
source

Source: https://habr.com/ru/post/989193/


All Articles