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

Async example? #111

Open
dragonattack opened this issue Jul 18, 2023 · 1 comment
Open

Async example? #111

dragonattack opened this issue Jul 18, 2023 · 1 comment
Labels

Comments

@dragonattack
Copy link

Can you please share a link to example of async dns query loop of 500 domains for example.

Of course, simply changing Dns\resolve($domain) in /examples/benchmark.php to:

        $host = Amp\Future\awaitFirst([
            Amp\async(fn() => Dns\resolve($domain, Dns\DnsRecord::A)),
        ]);

doesn't make any difference.

@trowski
Copy link
Member

trowski commented Nov 18, 2023

Apologies for the (very) late reply, this issue slipped under my radar.

To perform multiple queries simultaneously (or multiple of any operation which will await I/O), use Amp\async() in conjunction with Amp\Future\await() to await the set of futures created from async().

$listOfDomains = [...]; // List of domain names to query.

$futures = [];
foreach ($listOfDomains as $domain) {
    $futures[$domain] = Amp\async(Dns\resolve(...), $domain);
}

$resolvedRecords = Amp\Future\await($futures);

Note it may not be a good idea to initiate 500 DNS requests simultaneously. As with many simultaneous async operations, it may be a good idea to throttle the number of simultaneous requests in-flight. See amphp/sync and amphp/pipeline for tools to do just this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants