Non-blocking IO
Using the RxPHP extensions opens up quite a few possibilities. Its observables, operators, and subscribers/observers implementations are certainly powerful. What they don't provide, however, is asynchronicity. This is where the React library comes into play, by providing an event-driven, non-blocking I/O abstraction layer. Before we touch upon React, let's first lay out a trivial example of blocking versus non-blocking I/O in PHP.
We create a small beacon script that will merely generate some standard output (stdout) over time. Then, we will create a script that reads from the standard input (stdin) and see how it behaves when reading is done in the stream blocking and stream non-blocking mode.
We start by creating the beacon.php
file with the following content:
<?php $now = time(); while ($now + $argv[1] > time()) { echo 'signal ', microtime(), PHP_EOL; usleep(200000); // 0.2s }
The use of $argv[1]
hints that the file is intended to be run from console. Using $argv...