@@ -38,6 +38,7 @@ much any API that already uses Promises.
3838 * [ Promises] ( #promises )
3939 * [ Cancellation] ( #cancellation )
4040 * [ Timeout] ( #timeout )
41+ * [ Blocking] ( #blocking )
4142* [ Install] ( #install )
4243* [ Tests] ( #tests )
4344* [ License] ( #license )
@@ -197,6 +198,9 @@ promise which will start the actual operation once another operation is
197198completed. This means that this is handled entirely transparently and you do not
198199need to worry about this concurrency limit yourself.
199200
201+ If this looks strange to you, you can also use the more traditional
202+ [ blocking API] ( #blocking ) .
203+
200204#### Cancellation
201205
202206The returned Promise is implemented in such a way that it can be cancelled
@@ -254,6 +258,75 @@ $promise = Timer\timeout($q($url), 2.0, $loop);
254258Please refer to [ react/promise-timer] ( https://github.com/reactphp/promise-timer )
255259for more details.
256260
261+ #### Blocking
262+
263+ As stated above, this library provides you a powerful, async API by default.
264+ If, however, you want to integrate this into your traditional, blocking
265+ environment, you may want to look into also using
266+ [ clue/block-react] ( https://github.com/clue/php-block-react ) .
267+
268+ The resulting blocking code that awaits a number of concurrent HTTP requests
269+ could look something like this:
270+
271+ ``` php
272+ use Clue\React\Block;
273+
274+ $loop = React\EventLoop\Factory::create();
275+ $browser = new Clue\React\Buzz\Browser($loop);
276+
277+ $q = new Queue(10, null, function ($url) use ($browser) {
278+ return $browser->get($url);
279+ });
280+
281+ $promises = array(
282+ $q('http://example.com/'),
283+ $q('http://www.example.org/'),
284+ $q('http://example.net/'),
285+ );
286+
287+ try {
288+ $responses = Block\awaitAll($promises, $loop);
289+ // responses successfully received
290+ } catch (Exception $e) {
291+ // an error occured while performing the requests
292+ }
293+ ```
294+
295+ Similarly, you can also wrap this in a function to provide a simple API and hide
296+ all the async details from the outside:
297+
298+ ``` php
299+ /**
300+ * Concurrently downloads all the given URIs
301+ *
302+ * @param string[] $uris list of URIs to download
303+ * @return ResponseInterface[] map with a response object for each URI
304+ * @throws Exception if any of the URIs can not be downloaded
305+ */
306+ function download(array $uris)
307+ {
308+ $loop = React\EventLoop\Factory::create();
309+ $browser = new Clue\React\Buzz\Browser($loop);
310+
311+ $q = new Queue(10, null, function ($uri) use ($browser) {
312+ return $browser->get($uri);
313+ });
314+
315+ $promises = array();
316+ foreach ($uris as $uri) {
317+ $promises[$uri] = $q($uri);
318+ }
319+
320+ return Clue\React\Block\awaitAll($promises, $loop);
321+ }
322+ ```
323+
324+ Please refer to [ clue/block-react] ( https://github.com/clue/php-block-react#readme )
325+ for more details.
326+
327+ > Keep in mind that returning an array of response messages means that the whole
328+ response body has to be kept in memory.
329+
257330## Install
258331
259332The recommended way to install this library is [ through Composer] ( https://getcomposer.org ) .
0 commit comments