Giter Club home page Giter Club logo

Comments (9)

WyriHaximus avatar WyriHaximus commented on May 18, 2024 1

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.

WyriHaximus avatar WyriHaximus commented on May 18, 2024 1

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.

clue avatar clue commented on May 18, 2024 1

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.

WyriHaximus avatar WyriHaximus commented on May 18, 2024

That's easy, use react/dns

from async.

RobertEcker avatar RobertEcker commented on May 18, 2024

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.

WyriHaximus avatar WyriHaximus commented on May 18, 2024

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.

RobertEcker avatar RobertEcker commented on May 18, 2024

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.

RobertEcker avatar RobertEcker commented on May 18, 2024

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.

RobertEcker avatar RobertEcker commented on May 18, 2024

ok maybe i should check react/dns how it is done there to make it possible for all functions?

from async.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.