Guzzle async requests not really async?

Guzzle async requests not really async?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Guzzle async requests not really async?

You might expect that chaining sendAsync() calls within a generator and feeding them into GuzzleHttp\Promise\each_limit would fire off a pool of concurrent HTTP requests immediately. In your specific test case, however, the output logs reveal a sequential execution pattern: one request completes, then the next batch is queued. This behavior stems from how PHP's event loop interacts with the generator iterator and the underlying promise resolution mechanism.

The Generator Bottleneck

The core issue lies in the structure of your $iterator function. You are using yield to produce a promise for each request. However, generators in PHP are synchronous by default. When you call GuzzleHttp\Promise\each_limit, it iterates over your generator one step at a time.

In your current setup, every line inside the generator executes before the function yields control back to the caller. This means:

  1. The code creates the client.
  2. It constructs the request.
  3. It calls sendAsync($request).

At this exact moment, Guzzle initiates the network connection and sends the HTTP packet. Crucially, the generator does not stop there; it immediately proceeds to execute the next line in its own scope before yielding the promise object. Because the generator is blocking the execution flow while waiting for the next instruction (even if that instruction is just yield), the event loop cannot interleave other tasks effectively until the current iteration finishes.

However, the real constraint here is how each_limit consumes the iterator. It calls current() on your generator. If your generator hasn't finished executing its setup logic and hit the yield statement yet, the promise isn't ready to be passed into the pool. More importantly, if you rely on the default PHP execution model without an explicit event loop driver (like ReactPHP or Swoole), the script runs synchronously line-by-line.

The Missing Event Loop Integration

Guzzle 6 promises are designed to work seamlessly with an event loop. When you call $promise->wait(), Guzzle internally checks if a loop is running. If no external loop is active, it falls back to a synchronous implementation that waits for the request to complete before returning control to your code.

In your test environment (PHP 7.1.3), wait() blocks the main thread until the response is received. Because each_limit processes items sequentially from the iterator, and the iterator itself is being evaluated in a blocking context, the concurrency is effectively serialized. The "concurrency limit of 10" only applies to how many promises are kept open simultaneously if they were already created. In your generator, new requests are only instantiated after the previous iteration's yield point is reached, but the blocking nature of wait() inside the final resolution chain prevents the pipeline from filling up before the first batch finishes.

To achieve true concurrent behavior where 10 requests are queued and sent almost simultaneously, you must ensure that the generator yields control immediately after creating the promise, without executing subsequent logic in the same iteration. Additionally, for high-concurrency scenarios, it is often necessary to run the code within a ReactPHP event loop or use a specific Guzzle adapter that manages the underlying stream concurrency more aggressively.

Correcting the Flow

The fix involves ensuring that the generator yields immediately after creating the promise, allowing the event loop (or each_limit's internal logic) to pick up the pending request before the next iteration begins. You should also verify that you are not inadvertently blocking the execution flow with synchronous operations between the request creation and the yield.

If you are building a service where this pattern is critical, consider wrapping your generator in a ReactPHP loop using React\EventLoop\Timer or similar mechanisms to ensure non-blocking I/O. For many modern PHP applications, especially those running on frameworks like Laravel, utilizing the built-in async support provided by the framework's HTTP client or integrating with an external event loop driver ensures that the promises resolve in parallel rather than serially.

For more details on managing concurrency in Guzzle within a Laravel context, you can refer to https://laravelcompany.com.

Our network