我使用Guzzle 6来调用异步请求,然后使用React Promise/Deferred和Event循环,然后使用php-react-block来解析结果。
首先我发送http请求如下:
public function callService($endpoint){
$requestDeferred = new React\Promise\Deferred();
$requestParams = new Request("POST", $endpoint, $header, $message);
$client = new Client();//Guzzle http client
$client->sendAsync($requestParams)->then(
function (ResponseInterface $res) use($requestDeferred) {
// $res->getStatusCode() . "\n";
$requestDeferred->resolve($res->getBody()->getContents());
},
function (RequestException $e) use($requestDeferred) {
$requestDeferred->reject();
}
);
return $requestDeferred->promise();
}
我将此方法称为以下
$loop = React\EventLoop\Factory::create();
$requestPromise = $this->callService( $endpoint);
$responseXml = Clue\React\Block\await($requestPromise, $loop);// I want to block/wait to until promise is resolved and get resolved value.
但是当我调用Clue \ React \ Block \ await($ requestPromise,$ loop)时,系统会无限循环,任何承诺都无法解决。我还添加了queue-> run()方法来运行相关事件的方法(LibEvent)。但系统仍然无法循环。
为什么系统会无限循环?
感谢您的帮助