Comments (11)
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.
@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.
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.
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.
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.
@chrisleavoy It's not possible. You can't do it with the middle being unaware of promises.
from amp.
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.
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.
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.
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.
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)
- UnhandledFutureError is hard to debug HOT 2
- Migration guide for combinator functions HOT 5
- FiberError: Cannot switch fibers in current execution context on Laravel HOT 4
- 32 bit PHP: Return value of Amp\Loop\NativeDriver::now() must be of the type int, float returned HOT 5
- Functions should require callable instead of closures HOT 10
- Any way to create or get a context for coroutine. HOT 5
- Version confusion HOT 8
- Shouldn't Future::finally() pass the future data to the provided callback? HOT 7
- Event loop terminated without resuming the current suspension (the cause is either a fiber deadlock, or an incorrectly unreferenced/canceled watcher) HOT 3
- Future combinators HOT 8
- When to not to use `async`?
- Comparison with ReactPHP HOT 8
- The docs not up to date? HOT 3
- Where is this `any()` function?
- Interception of pause / resume future HOT 2
- Add Payload::readEntireBody? HOT 3
- Symfony Messenger Integration HOT 4
- Using AMPHP with Psalm HOT 5
- onComplete event for async? HOT 9
- [Feature Request] - `awaitAll` throwing `CompositeException`
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 amp.