Code Monkey home page Code Monkey logo

Comments (11)

staabm avatar staabm commented on August 28, 2024

In case you like to wait "synchronously" for a promise, just yield it.

\Amp\run(function() {
    $promise = \Amp\Dns\resolve('google.ca');
    $result = yield $promise;
    var_dump($result);
});

from amp.

bwoebi avatar bwoebi commented on August 28, 2024

@staabm He knows that…

By the way, your icicle example works as expected (or not?) as it does not wait on the same loop, but once on the icicle loop, once on the amp loop.

The main issue with wait() inside already async context is that it leads to heavy recursion which may or may not fail depending on whether a certain generator is already in your stack trace (cannot resume a current running Generator). It's a large pitfall which may only happen in more complex applications.
Also, the VM only continues after a wait() if all calls afterwards to wait() are resolved (due to the way stacktraces work. That's what we have Generators for!).

Thus we forbid this explicitly. You have to yield Promises or yield from Generators. That's an inherent requirement which we cannot lift without making Amp very unstable.

from amp.

chrisleavoy avatar chrisleavoy commented on August 28, 2024

Surprisingly, my icicle example works great. Amp\Dns\Resolve yields just fine inside the Icicle loop.

I can't leverage yield, becaus yield isn't allowed in the same function with a return. And I can't pass promises up the entire stack where the Amp context is created because I have a middle layer I don't want to change. So unless I'm missing something, its impossible to use a generator inside an \Amp\wait() context, which is the heart of my issue.

IE this is not allowed:

\Amp\run(function() {
    function resolve() {
        $promise = \Amp\Dns\resolve('google.ca');
        $result = (yield $promise);
        return ($result);
    }
    var_dump(resolve());
});

PHP Fatal error: Generators cannot return values using "return"

from amp.

bwoebi avatar bwoebi commented on August 28, 2024

Looks like you are using PHP 5.

In this case you may yield new \Amp\CoroutineResult($result); instead of a return.

P.s.: that icicle works great is that you example is so simple and you are effectively using two different loops here. In your problematic gist, it's all one same loop, thus problematic.

from amp.

chrisleavoy avatar chrisleavoy commented on August 28, 2024

That doesn't work either. It just moves the problem.

\Amp\run(function() {
    function resolve() {
        $promise = \Amp\Dns\resolve('google.ca');
        $result = (yield $promise);
        yield new \Amp\CoroutineResult($result);
    }
    $generator = resolve();
    // cant wait:
    \Amp\wait(\Amp\resolve($generator));
   // can't (yield resolve()); because then I can't return to my parent which is synchronous
});

I can't figure out how to use yield where inside the call stack there is a return that waits synchronously inside an Amp context. :) omg my head hurts.

from amp.

kelunik avatar kelunik commented on August 28, 2024

@chrisleavoy It's not possible. You can't do it with the middle being unaware of promises.

from amp.

bwoebi avatar bwoebi commented on August 28, 2024

oh, you want to explicitly run synchronously (i.e. block everything else possibly waiting? right?).

In this case you have to replace the reactor around that:

$old = \Amp\reactor();
\Amp\reactor(\Amp\driver())
$result = \Amp\wait(resolve());
\Amp\reactor($old);

from amp.

chrisleavoy avatar chrisleavoy commented on August 28, 2024

Thats not quite what I need. In my example gist, (updated: https://gist.github.com/chrisleavoy/9c2386e3b408399c4ba2e5a4db055888) I'll have 4 or more parallel "plugins" running asynchronously. Each plugin has a synchronous function and waits for the underlying DNS query, which should yield in the outer Amp reactor letting other plugins run when ready. If I create multiple Amp reactors, the yield doesn't work. In my Icicle example (https://gist.github.com/chrisleavoy/18b4032402d7b2d7f688655fe28eab60) parallel Amp\waits inside one outer Icicle reactor, seems to work. Ideally I'd like to get this working without Icicle though.

from amp.

bwoebi avatar bwoebi commented on August 28, 2024

https://gist.github.com/bwoebi/7cba5729647b7ee76fcc337a06e0a5ed

You must keep your functions async between each other. You must call Amp\wait() only outside the async tree.
There is no way around that; this is inherent to Amp and Icicle.

Your Icicle/Amp mix just seems to work now, but it will fail in much more complicated setups.

from amp.

chrisleavoy avatar chrisleavoy commented on August 28, 2024

Darn.

Thanks so much for the schooling. Much appreciated. I was really trying not to chain the entire call stack but it sounds like its not possible. On closer debugging, my Iclcle/Amp mix, wasn't actually async. I had only made it so overly complicated that I wasn't tracing it properly. πŸ˜†

from amp.

bwoebi avatar bwoebi commented on August 28, 2024

No problem, people who have coded always sync before all have similar problems...

Happy to help out and make you understand the concepts properly!

from amp.

Related Issues (20)

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.