Comments (9)
I meant functions like dns_get_record
will block the event loop and thus everything else running in it. Which is why I suggested using react/dns
. Let me rewrite your initial code with it, maybe that's clearer:
<?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));
from async.
We provide react/child-process
to do such things, and I have a package or two allowing you to use child processes to run functions outside the current process. But that is not performant compared to staying in the current process alternatives that hook directly into the event loop.
The whole goal of reactphp is doing these things non-blocking at such a speed it looks like they are run in parallel.
from async.
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 dns_get_record()
function like this:
$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 👍
from async.
That's easy, use react/dns
from async.
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
from async.
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));
from async.
thank you for the example
regarding "non of these functions can block": i do not fully understand because "dns_get_record" (in my example) blocks the next one/ones (because of waiting of the response) - or did you mean your example (then it is clear)
from async.
Thank you again @WyriHaximus
That means the stackoverflow-post is correct? So reactphp is not forking processes to make it faster in general?
from async.
ok maybe i should check react/dns how it is done there to make it possible for all functions?
from async.
Related Issues (14)
- await() error in SimpleFiber.php HOT 17
- Fibers + Cancellation HOT 2
- Use 0.x versions HOT 5
- Error: event_base_loop reentrant invocation HOT 9
- await() doesn't work with streaming HTTP response body HOT 9
- Allow `iterable` instead of `array` for `parallel()`, `series()` and `waterfall()` HOT 1
- Cancellation semantics for `async()` and `coroutine()` HOT 2
- Support Promise v3 HOT 1
- Using await in a destructor HOT 7
- Improve error reporting when incorrectly using `await()` (Value of type null is not callable in src/SimpleFiber.php:66) HOT 6
- Struggling with the simplest example HOT 1
- reactphp/async
- \React\Async\async did not invokes in on('data') event HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from async.