Re: PHP True Async RFC - Stage 2
| From: | Edmond Dantes | Date: | Tue, 18 Mar 2025 13:52:48 +0000 |
| Subject: | Re: PHP True Async RFC - Stage 2 | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-126824@lists.php.net to get a copy of this message | ||
Oops, I made a mistake in the logic of
Scope and coroutines.
According to the RFC, the following code behaves differently:
```php
currentScope()->spawn ... // This coroutine belongs to the Scope
spawn ... // This one is a child coroutine
```
I was sure that I had checked all the major edge cases. Sorry.
This will be fixed soon.
P.S. + 1 example:
<?php
declare(strict_types=1);
use Async\Scope;
use function Async\currentScope;
function fetchUrl(string $url): string {
$ctx = stream_context_create(['http' => ['timeout' => 5]]);
return file_get_contents($url, false, $ctx);
}
function fetchAllUrls(array $urls): array
{
$futures = [];
foreach ($urls as $url) {
$futures[$url] = (spawn fetchUrl($url))->getFuture();
}
await currentScope();
$results = [];
foreach ($futures as $url => $future) {
$results[$url] = $future->getResult();
}
return $results;
}
$urls = [
'https://example.com',
'https://php.net',
'https://openai.com'
];
$results = await spawn fetchAllUrls($urls);
print_r($results);
---
Ed.
>