4

Phil Sturgeon wrote this article about benchmarking.

What I am concerned with is this technology called ReactPHP.

I have been to their GitHub, but still cannot wrap my mind around it without an example.

Thankfully, I can use Phil's repo.

At line 12, Phil created a loop with ReactPHP.

https://github.com/philsturgeon/nonblockingbro/blob/master/p2-async.php#L12

Then he started to use the loop at line 24

https://github.com/philsturgeon/nonblockingbro/blob/master/p2-async.php#L24

My questions are:

  • what is the purpose of this $loop?
  • It says on the ReactPHP, it is a simple web server. Does that mean it replaces nginx or Apache?
  • When is a good time to use ReactPHP or any similar technology? When is NOT a good time to use it?

I am trying to learn about this technology, so if I have worded this question badly, do let me know so I can rewrite this.

EDIT:

Since I am getting feedback that the question is too broad, let me narrow my question and make it refer to a code example.

Taking the code sample from https://github.com/philsturgeon/nonblockingbro/blob/master/p2-async.php#L12,

which I have duplicated below. What does $loop do?

$loop = React\EventLoop\Factory::create();

$dnsResolverFactory = new React\Dns\Resolver\Factory();
$dnsResolver = $dnsResolverFactory->createCached('8.8.8.8', $loop);

$factory = new React\HttpClient\Factory();
$client = $factory->create($loop, $dnsResolver);


echo "Page number, Time taken";
for ($page = 1; $page <= $total_page; $page++) {

    $loop->addTimer(0.001, function($timer) use ($client, $page) {
        $buffer = '';
        $request = $client->request('GET', 'http://fantasy.premierleague.com/my-leagues/303/standings/?ls-page='.$page);
        $request->on('response', function($response) use (&$buffer) {
            $response->on('data', function($data) use (&$buffer) {
                $buffer .= $data;
            });
        });
        $request->on('end', function() use (&$buffer, $page) {

            \phpQuery::newDocument($buffer);

            foreach (pq('.ismStandingsTable tr') as $data) {
                foreach (pq('td', $data) as $key => $val) {
                    if ($key == 2) {
                        // print pq($val)->text();
                    }
                }
            }

            $time_end = microtime(true);
            $execution_time = $time_end - $GLOBALS['time_start'];
            echo ("\n".$page.", ".$execution_time);

        });
        $request->end();
    });
}

$loop->run();
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Kim Stacks
  • 10,202
  • 35
  • 151
  • 282

2 Answers2

3

1 - what is the purpose of this $loop?

Well, the $loop var is used for keep all closures functions into an array, to be executed later asynchronously.

2 - It says on the ReactPHP, it is a simple web server. Does that mean it replaces nginx or Apache?

ReactPHP will not replace or fight with (Apache2 | Nginx) , It will aggregate some issues to CLI process in most of cases.

3 - When is a good time to use ReactPHP or any similar technology? When is NOT a good time to use it?

I really don't use ReactPHP on production server, but it's has been very successful to perform some automate batch process or testing some issues in staging server.

Markomafs
  • 546
  • 4
  • 19
0

The loop does exactly what it says. It loops. Just like node.js it continuously repeats the infinte cycle and waits for some states of attached tasks to change. These changes emit events that can be handled exactly as the occur. This gives you the opportunity to react to external events (another process in your os, another Webservice or likewise). This is called reactive programming.

Possible use cases are socket servers (w/o ngjnx or Apache) that wait for clients to connect (e.g. webchat) or saving current stock prices from stock exchange that you are connected to via a socket connection.

bastianowicz
  • 358
  • 2
  • 9