简体   繁体   中英

How the Amphp parallel works?

I was reading about amphp, and I had a doubt about parallel

Example code:

<?php

require __DIR__ . '/vendor/autoload.php';

$client = new Amp\Artax\DefaultClient;
$promises = [];

$urls = [
    "http://google.com.br",
    "http://facebook.com.br",
    "http://xat.com/",
    "http://kobra.ninja/",
    "http://wikipedia.com",
    "http://manualdomundo.com.br",
    "http://globo.com",
    "http://gmail.com"
];

foreach ($urls as $url) {
    $promises[$url] = Amp\call(function () use ($client, $url) {
        // "yield" inside a coroutine awaits the resolution of the promise
        // returned from Client::request(). The generator is then continued.
        $response = yield $client->request($url);

        // Same for the body here. Yielding an Amp\ByteStream\Message
        // buffers the entire message.
        $body = yield $response->getBody();
        echo $url;
        return $url;
    });
}

$responses = Amp\Promise\wait(Amp\Promise\all($promises));

Is this code running all curl, or waiting for 1 to perform another?

As you can see from Amp codebase , it will run all your requests inside the same event loop.

Given your requests are being yielded ( yield $client->request($url) ), and they are being yielded inside the same event loop, they are being dispatched concurrently.

I recommend you reading this article through , specially the "Async development: how Amp framework works" section. I think it will make it a bit clearer how the engine works behind the scenes.

Hope this was helpful. Cheers! :)

This is using stream_select functionality. It is not running in parallel in the conventional way you are thinking of. It works by registering a callback to be invoked once streams are readable / writable and then returning when the specific promise you are waiting for is done. All other promises that are resolved in the meantime are completed and cached in their respective promise waiting for you to unwrap the value using yield or Promise::onResolve . Amp's event loop is what is managing the multiple sockets and handling the concurrency for you.

If you want a basic example of how this works, I put a sample project on GitHub that is two classes, but is based on Curl instead of stream_select : https://github.com/kwhat/requestful

The first 7 methods are all that is required to setup the promise interface. All of the magic here is based on the two callbacks passed to the constructor as well as the wrappers around the then/cancel callbacks.

The sendRequestAsync method is how the concurrent Curl requests are created. The magic all happens in the callback for wait() on any promise, which calls an anonymous function that intern calls the tick method in a loop. That tick() method is what resolves all of the promises, regardless which one you call wait on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM