mirror of
https://github.com/danog/php-tokio.git
synced 2024-11-26 12:24:58 +01:00
Improve examples
This commit is contained in:
parent
98ceee07e4
commit
1457cc9539
24
README.md
24
README.md
@ -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!
|
||||||
|
@ -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!
|
||||||
|
@ -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);
|
Loading…
Reference in New Issue
Block a user