Asynchronous 对于并发请求,多个Guzzle客户端能否共享同一个处理程序?

Asynchronous 对于并发请求,多个Guzzle客户端能否共享同一个处理程序?,asynchronous,concurrency,parallel-processing,guzzle,Asynchronous,Concurrency,Parallel Processing,Guzzle,我在一个项目中使用Guzzle,其中各种服务都由自己的Guzzle客户端表示。现在我知道了如何用狂饮来激发并发请求;然而,我们发现,当使用多个Guzzle客户端时,这就不那么琐碎了。我将在下面详细说明。但我的问题是:不同的Guzzle客户端是否应该/允许共享同一个多处理器?我找不到这方面的信息 因此,要详细说明一下:当您想要与一个Guzzle客户端并行地触发多个请求时,这相对容易——或者至少有很好的文档记录: $client = new GuzzleHttp\Client(['base_uri'

我在一个项目中使用Guzzle,其中各种服务都由自己的Guzzle客户端表示。现在我知道了如何用狂饮来激发并发请求;然而,我们发现,当使用多个Guzzle客户端时,这就不那么琐碎了。我将在下面详细说明。但我的问题是:不同的Guzzle客户端是否应该/允许共享同一个多处理器?我找不到这方面的信息

因此,要详细说明一下:当您想要与一个Guzzle客户端并行地触发多个请求时,这相对容易——或者至少有很好的文档记录:

$client = new GuzzleHttp\Client(['base_uri' => 'https://example.com']);

$request = new Request('GET', '/some-call');
$promises[] = $client->sendAsync($request);
$request = new Request('GET', '/other-call');
$promises[] = $client->sendAsync($request);

$results = \GuzzleHttp\Promise\settle($promises)->wait();
这很有效。请求将被同时激发和处理

但是在我的项目中有多个客户端,每个客户端都有自己的base_uri和其他设置(后者留在这里是为了保持干净)。因此:

结果证明,不起作用。第二个请求只有在第一个请求完全完成后才会被触发。 现在我不确定这是怎么回事?也许不是很多人一开始就这样使用它。 尽管如此,我还是找到了一种方法,让客户共享同一个处理程序:

$handler = new GuzzleHttp\Handler\CurlMultiHandler();

$client1 = new GuzzleHttp\Client(['base_uri' => 'https://service1.example.com', 'handler' => GuzzleHttp\HandlerStack::create($handler)]);
$client2 = new GuzzleHttp\Client(['base_uri' => 'https://service2.example.com', 'handler' => GuzzleHttp\HandlerStack::create($handler)]);

$request = new Request('GET', '/some-call');
$promises[] = $client1->sendAsync($request);
$request = new Request('GET', '/other-call');
$promises[] = $client2->sendAsync($request);

$results = \GuzzleHttp\Promise\settle($promises)->wait();
通过这种方式,请求再次被同时激发和处理

我的问题是:我不知道是否应该这样做?安全吗?如果是这样的话,他们可能会共用同一个手柄吗

希望有内行的人能回答

$handler = new GuzzleHttp\Handler\CurlMultiHandler();

$client1 = new GuzzleHttp\Client(['base_uri' => 'https://service1.example.com', 'handler' => GuzzleHttp\HandlerStack::create($handler)]);
$client2 = new GuzzleHttp\Client(['base_uri' => 'https://service2.example.com', 'handler' => GuzzleHttp\HandlerStack::create($handler)]);

$request = new Request('GET', '/some-call');
$promises[] = $client1->sendAsync($request);
$request = new Request('GET', '/other-call');
$promises[] = $client2->sendAsync($request);

$results = \GuzzleHttp\Promise\settle($promises)->wait();