Improve examples

This commit is contained in:
Daniil Gentili 2023-08-27 20:40:41 +02:00
parent 98ceee07e4
commit 1457cc9539
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
3 changed files with 33 additions and 28 deletions

View File

@ -22,17 +22,19 @@ require 'vendor/autoload.php';
Client::init(); Client::init();
$test = function (string $url): void { function test(int $delay): void {
$url = "https://httpbin.org/delay/$delay";
$t = time(); $t = time();
echo "Making async parallel reqwest to $url (time $t)...".PHP_EOL; echo "Making async reqwest to $url that will return after $delay seconds...".PHP_EOL;
Client::get($url);
$t = time() - $t; $t = time() - $t;
echo "Got response from $url after ~".$t." seconds!".PHP_EOL; echo "Got response from $url after ~".$t." seconds!".PHP_EOL;
}; };
$futures = []; $futures = [];
$futures []= async($test(...), "https://httpbin.org/delay/5"); $futures []= async(test(...), 5);
$futures []= async($test(...), "https://httpbin.org/delay/5"); $futures []= async(test(...), 5);
$futures []= async($test(...), "https://httpbin.org/delay/5"); $futures []= async(test(...), 5);
await($futures); await($futures);
``` ```
@ -49,12 +51,12 @@ cd examples/reqwest && \
Result: Result:
``` ```
Making async parallel reqwest to https://httpbin.org/delay/5 (time 1693160463)... Making async reqwest to https://httpbin.org/delay/5 that will return after 5 seconds...
Making async parallel reqwest to https://httpbin.org/delay/5 (time 1693160463)... Making async reqwest to https://httpbin.org/delay/5 that will return after 5 seconds...
Making async parallel reqwest to https://httpbin.org/delay/5 (time 1693160463)... Making async reqwest to https://httpbin.org/delay/5 that will return after 5 seconds...
Got response from https://httpbin.org/delay/5 after ~6 seconds! Got response from https://httpbin.org/delay/5 after ~5 seconds!
Got response from https://httpbin.org/delay/5 after ~6 seconds! Got response from https://httpbin.org/delay/5 after ~5 seconds!
Got response from https://httpbin.org/delay/5 after ~6 seconds! Got response from https://httpbin.org/delay/5 after ~5 seconds!
``` ```
See the [source code](https://github.com/danog/php-tokio/tree/master/examples/reqwest) of the example for more info on how it works! See the [source code](https://github.com/danog/php-tokio/tree/master/examples/reqwest) of the example for more info on how it works!

View File

@ -14,17 +14,19 @@ require 'vendor/autoload.php';
Client::init(); Client::init();
$test = function (string $url): void { function test(int $delay): void {
$url = "https://httpbin.org/delay/$delay";
$t = time(); $t = time();
echo "Making async parallel reqwest to $url (time $t)...".PHP_EOL; echo "Making async reqwest to $url that will return after $delay seconds...".PHP_EOL;
Client::get($url);
$t = time() - $t; $t = time() - $t;
echo "Got response from $url after ~".$t." seconds!".PHP_EOL; echo "Got response from $url after ~".$t." seconds!".PHP_EOL;
}; };
$futures = []; $futures = [];
$futures []= async($test(...), "https://httpbin.org/delay/5"); $futures []= async(test(...), 5);
$futures []= async($test(...), "https://httpbin.org/delay/5"); $futures []= async(test(...), 5);
$futures []= async($test(...), "https://httpbin.org/delay/5"); $futures []= async(test(...), 5);
await($futures); await($futures);
``` ```
@ -41,12 +43,12 @@ cd examples/reqwest && \
Result: Result:
``` ```
Making async parallel reqwest to https://httpbin.org/delay/5 (time 1693160463)... Making async reqwest to https://httpbin.org/delay/5 that will return after 5 seconds...
Making async parallel reqwest to https://httpbin.org/delay/5 (time 1693160463)... Making async reqwest to https://httpbin.org/delay/5 that will return after 5 seconds...
Making async parallel reqwest to https://httpbin.org/delay/5 (time 1693160463)... Making async reqwest to https://httpbin.org/delay/5 that will return after 5 seconds...
Got response from https://httpbin.org/delay/5 after ~6 seconds! Got response from https://httpbin.org/delay/5 after ~5 seconds!
Got response from https://httpbin.org/delay/5 after ~6 seconds! Got response from https://httpbin.org/delay/5 after ~5 seconds!
Got response from https://httpbin.org/delay/5 after ~6 seconds! Got response from https://httpbin.org/delay/5 after ~5 seconds!
``` ```
See the [source code](https://github.com/danog/php-tokio/tree/master/examples/reqwest) of the example for more info on how it works! See the [source code](https://github.com/danog/php-tokio/tree/master/examples/reqwest) of the example for more info on how it works!

View File

@ -9,17 +9,18 @@ require 'vendor/autoload.php';
Client::init(); Client::init();
$test = function (string $url): void { function test(int $delay): void {
$url = "https://httpbin.org/delay/$delay";
$t = time(); $t = time();
echo "Making async parallel reqwest to $url (time $t)...".PHP_EOL; echo "Making async reqwest to $url that will return after $delay seconds...".PHP_EOL;
var_dump(Client::get($url)); Client::get($url);
$t = time() - $t; $t = time() - $t;
echo "Got response from $url after ~".$t." seconds!".PHP_EOL; echo "Got response from $url after ~".$t." seconds!".PHP_EOL;
}; };
$futures = []; $futures = [];
$futures []= async($test(...), "https://httpbin.org/delay/5"); $futures []= async(test(...), 5);
$futures []= async($test(...), "https://httpbin.org/delay/5"); $futures []= async(test(...), 5);
$futures []= async($test(...), "https://httpbin.org/delay/5"); $futures []= async(test(...), 5);
await($futures); await($futures);