我使用Guzzle 6 调用异步请求,然后使用React Promise/Deferred和 Event loop ,然后使用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)的方法。但是系统仍然无限循环。
为什么系统无限循环?
感谢您的帮助