-
-
Notifications
You must be signed in to change notification settings - Fork 18
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
Basic question 'bout reactphp #58
Comments
That's easy, use |
Hi @WyriHaximus, I know react/dns ... DNS was just an example ... it is more about a general question: how to execute own functions etc. in parallel with reactphp |
Keep in mind that non of these functions can block, you'd still have to use non-blocking I/O functions for anything you do in here, but it comes down to: $promises = [];
foreach (range(1, 13) as $i) {
$promises[] = (async(function (int $i) {
return $i;
}))($i);
}
var_export(await($promises)); |
thank you for the example |
I meant functions like <?php
$result = [];
$domains = ['domain1.com', 'domain2.xyz'];
foreach ($domains as $domain) {
$result[] = (async(function (string $domain) {
return await($dns->resolve(domain));
}))(domain);
}
var_dump(await($result)); That could be done simpler like this: <?php
$result = [];
$domains = ['domain1.com', 'domain2.xyz'];
foreach ($domains as $domain) {
$result[] = $dns->resolve(domain);
}
var_dump(await($result)); |
Thank you again @WyriHaximus That means the stackoverflow-post is correct? So reactphp is not forking processes to make it faster in general? |
ok maybe i should check react/dns how it is done there to make it possible for all functions? |
We provide The whole goal of reactphp is doing these things non-blocking at such a speed it looks like they are run in parallel. |
As @WyriHaximus pointed out, you can execute all blocking functions in a separate child process to avoid blocking the main process. For instance, you can use https://github.com/clue/reactphp-pq to execute the $executor = new Clue\React\Pq\Executor();
$dns_get_record = $executor->fun('dns_get_record');
$dns_get_record($domain)->then(function ($result) {
var_dump($result);
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
}); That said, I agree that this should usually be used as a last resort only as execution in child processes incurs some noticeable overhead (YMMV). For many common APIs, ReactPHP provides async alternatives that are implemented in pure PHP (see https://github.com/reactphp/reactphp/wiki/Users). For DNS queries, this would be the DNS component as suggested above. I believe this has been answered, so I'm closing this for now. Please come back with more details if this problem persists and we can always reopen this 👍 |
Hi,
Sorry, for this - again - basic question about reactphp
To make my question more clear, I use this DNS lookup example:
Can above example executed in parallel (to make it faster) with reactphp or not (and if yes, how with async? or event-loop? can you give me an additional hint)?
If I understand this stackoverflow post correctly, it can not (but I do not think so).
Thank you again.
The text was updated successfully, but these errors were encountered: