2016-04-20 19 views
0

Wie kann ich Methoden in symfony2.7 Like asynchron aufrufen.Asynchrone Aufrufe in symfony2

Ich muss Daten abrufen, die 4 verschiedene API-Verbindungen machen. Das Problem ist eine langsame Antwort von meiner Anwendung, da PHP synchron ist, so dass es auf die Antwort von der gesamten API warten und dann die Daten rendern muss.

class MainController{ 

    public function IndexAction(){ 
    // make Asynchronous Calls to GetFirstAPIData(), GetSecondAPIData(), GetThridAPIData() 
    } 
    public function GetFirstAPIData(){ 
    // Get data 
    } 

    public function GetSecondAPIData(){ 
    // Get data 
    } 

    public function GetThridAPIData(){ 
    // Get data 
    } 
} 
+0

Sie könnten nachsehen: http://php.net/manual/en/function.proc-open.php – Marcus

+0

Siehst du reactphp ?, siehe http://reactphp.org/ – ghanbari

Antwort

2

können Sie verwenden guzzle für das, vor allem, wenn wir reden über http basierte APIs. Guzzle ist ein Web-Client, der in Asynchron-Anrufe gebaut hat

Der Code etwas würde wie folgt aussehen:. (Aus dem docs)

$client = new Client(['base_uri' => 'http://httpbin.org/']); 

// Initiate each request but do not block 
$promises = [ 
    'image' => $client->getAsync('/image'), 
    'png' => $client->getAsync('/image/png'), 
    'jpeg' => $client->getAsync('/image/jpeg'), 
    'webp' => $client->getAsync('/image/webp') 
]; 

// Wait on all of the requests to complete. Throws a ConnectException 
// if any of the requests fail 
$results = Promise\unwrap($promises); 

// Wait for the requests to complete, even if some of them fail 
$results = Promise\settle($promises)->wait(); 

// You can access each result using the key provided to the unwrap 
// function. 
echo $results['image']->getHeader('Content-Length'); 
echo $results['png']->getHeader('Content-Length'); 

In diesem Beispiel sind alle vier Anforderungen parallel ausgeführt werden. Hinweis: Nur IO async ist nicht die Behandlung der Ergebnisse. Aber das ist wahrscheinlich das, was du willst.

Verwandte Themen