Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve documentation and examples #47

Merged
merged 1 commit into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ built on top of [ReactPHP](https://reactphp.org/).
Let's say you crawl a page and find that you need to send 100 HTTP requests to
following pages which each takes `0.2s`. You can either send them all
sequentially (taking around `20s`) or you can use
[ReactPHP](https://reactphp.org) to concurrently request all your pages at the
[ReactPHP](https://reactphp.org/) to concurrently request all your pages at the
same time. This works perfectly fine for a small number of operations, but
sending an excessive number of requests can either take up all resources on your
side or may get you banned by the remote side as it sees an unreasonable number
Expand Down Expand Up @@ -84,12 +84,14 @@ $q = new Clue\React\Mq\Queue(3, null, function ($url) use ($browser) {
foreach ($urls as $url) {
$q($url)->then(function (Psr\Http\Message\ResponseInterface $response) use ($url) {
echo $url . ': ' . $response->getBody()->getSize() . ' bytes' . PHP_EOL;
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
}

```

See also the [examples](examples).
See also the [examples](examples/).

## Usage

Expand Down Expand Up @@ -292,6 +294,8 @@ $promise = Queue::all(3, $urls, function ($url) use ($browser) {

$promise->then(function (array $responses) {
echo 'All ' . count($responses) . ' successful!' . PHP_EOL;
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
```

Expand Down Expand Up @@ -368,6 +372,8 @@ $promise = Queue::any(3, $urls, function ($url) use ($browser) {

$promise->then(function (ResponseInterface $response) {
echo 'First response: ' . $response->getBody() . PHP_EOL;
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
```

Expand Down
5 changes: 2 additions & 3 deletions examples/01-http.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@
function (Psr\Http\Message\ResponseInterface $response) use ($url) {
echo $url . ' has ' . $response->getBody()->getSize() . ' bytes' . PHP_EOL;
},
function (Exception $e) use ($url) {
echo $url . ' failed: ' . $e->getMessage() . PHP_EOL;
function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
}
);
}

5 changes: 2 additions & 3 deletions examples/02-http-all.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ function ($responses) {
echo $url . ' has ' . $response->getBody()->getSize() . ' bytes' . PHP_EOL;
}
},
function ($e) {
echo 'An error occurred: ' . $e->getMessage() . PHP_EOL;
function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
}
);

5 changes: 2 additions & 3 deletions examples/03-http-any.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ function (Psr\Http\Message\ResponseInterface $response) use ($url) {
function ($url) {
echo 'First successful URL is ' . $url . PHP_EOL;
},
function ($e) {
echo 'An error occurred: ' . $e->getMessage() . PHP_EOL;
function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
}
);