What does the $ loop do in this code example?

Phil Sturgeon wrote this article on benchmarking.

I am engaged in this technology called ReactPHP.

I have been to their GitHub, but I can’t get around it without an example.

Fortunately, I can use the fil repo.

On line 12, Phil created a loop with ReactPHP.

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

Then he started using the loop on line 24

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

My questions:

  • What is the purpose of this $loop?
  • ReactPHP says it is a simple web server. Does this mean that it replaces nginx or Apache?
  • When is the best time to use ReactPHP or any similar technology? When is not the right time to use it?

, , , , .

EDIT:

, , .

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

. $loop?

$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();
+4
2

1 - $?

, $loop var , , .

2 - ReactPHP, -. , nginx Apache?

ReactPHP (Apache2 | Nginx). CLI.

3 - ReactPHP ? ?

ReactPHP , .

+3

, . . , node.js, infinte , . , , . ( os, Web- ). .

( ngjnx Apache), (, -) , .

0

All Articles