Comments (9)
Forgotten
async()
call forcesSimpleFiber::suspend()
to callLoop::run()
Line 44 in ff11a7a
But if
Loop::run()
is already called beforeawait()
is called, then this causes various problems.
Another example<?php use React\EventLoop\Loop; use React\Promise\Deferred; use function React\Async\await; require __DIR__ . '/vendor/autoload.php'; Loop::futureTick(/* missed await */ function () { $deferred = new Deferred(); Loop::addTimer(0, static fn() => $deferred->resolve()); await($deferred->promise()); }); Loop::futureTick((static fn() => 0));
PHP Fatal error: Uncaught RuntimeException: Can't shift from an empty datastructure in /vendor/react/event-loop/src/Tick/FutureTickQueue.php:46 Stack trace: #0 /vendor/react/event-loop/src/Tick/FutureTickQueue.php(46): SplQueue->dequeue() #1 /vendor/react/event-loop/src/ExtEventLoop.php(203): React\EventLoop\Tick\FutureTickQueue->tick() #2 /vendor/react/event-loop/src/Loop.php(55): React\EventLoop\ExtEventLoop->run() #3 [internal function]: React\EventLoop\Loop::React\EventLoop\{closure}() #4 {main} thrown in /vendor/react/event-loop/src/Tick/FutureTickQueue.php on line 46
As a result, we do not know the problem file and line. It's very easy to miss
async()
in future code.
We MIGHT always wrap loop handers in an async()
call in the future. For now, you have to do this yourself, and yes that means you might miss one.
But I'll also open a PR to test this package against all event loops we support because honestly I only develop and tested it against ext-uv
. Also @clue has looked into if we still perse need the future ticks and we might drop them from the implementation. I'll also PR your example as a test, if it's not already in there in a maybe slightly different way.
Maybe just throw an exception instead of calling
Loop::run()
?
Because that Fiber
constructor argument triggers when the main Fiber starts.
from async.
Thanks, there is no problem with
async()
. Does this mean that any function that containsawait()
should be wrapped withasync()
?
Excellent input! The await()
function can indeed be considered "blocking", so if you're using it from an event loop timer, event listener, or promise callback, you should always wrap it in an async()
function. I've just filed #26 to add documentation for this behavior.
Technically, we could also wrap this automatically for each callback, but this incurs a significant overhead. This is especially true as we would have to do this for each callback even if it does not use await()
internally, because there's no way to tell from the outside.
At the moment, explicitly using async()
in these cases instead seems like a reasonable compromise between ease of use and performance. This is still an early development version and I'm curious to see how the ecosystem and performance considerations evolve over time! 👍
from async.
Which version of this package are you using?
from async.
@WyriHaximus,
react/async: dev-main (ff11a7a)
react/event-loop: v1.2.0
Tested on PHP 8.1.0 and PHP 8.1.1
from async.
Here you tried:
<?php
use React\EventLoop\Loop;
use React\Promise\Deferred;
use function React\Async\async;
use function React\Async\await;
require __DIR__ . '/vendor/autoload.php';
Loop::addTimer(0, async(function () {
$deferred = new Deferred();
Loop::addTimer(0, static fn() => $deferred->resolve());
await($deferred->promise());
}));
Or using https://github.com/reactphp/promise-timer#sleep
<?php
use React\EventLoop\Loop;
use React\Promise\Deferred;
use function React\Async\async;
use function React\Async\await;
use React\Promise\Timer\sleep;
require __DIR__ . '/vendor/autoload.php';
Loop::addTimer(0, async(function () {
await(sleep(0));
}));
from async.
Thanks, there is no problem with async()
.
Does this mean that any function that contains await()
should be wrapped with async()
?
from async.
Forgotten async()
call forces SimpleFiber::suspend()
to call Loop::run()
Line 44 in ff11a7a
But if
Loop::run()
is already called before await()
is called, then this causes various problems.
Another example
<?php
use React\EventLoop\Loop;
use React\Promise\Deferred;
use function React\Async\await;
require __DIR__ . '/vendor/autoload.php';
Loop::futureTick(/* missed await */ function () {
$deferred = new Deferred();
Loop::addTimer(0, static fn() => $deferred->resolve());
await($deferred->promise());
});
Loop::futureTick((static fn() => 0));
PHP Fatal error: Uncaught RuntimeException: Can't shift from an empty datastructure in /vendor/react/event-loop/src/Tick/FutureTickQueue.php:46
Stack trace:
#0 /vendor/react/event-loop/src/Tick/FutureTickQueue.php(46): SplQueue->dequeue()
#1 /vendor/react/event-loop/src/ExtEventLoop.php(203): React\EventLoop\Tick\FutureTickQueue->tick()
#2 /vendor/react/event-loop/src/Loop.php(55): React\EventLoop\ExtEventLoop->run()
#3 [internal function]: React\EventLoop\Loop::React\EventLoop\{closure}()
#4 {main}
thrown in /vendor/react/event-loop/src/Tick/FutureTickQueue.php on line 46
As a result, we do not know the problem file and line. It's very easy to miss async()
in future code.
Maybe just throw an exception instead of calling Loop::run()
?
from async.
@Provoker Which version of ext-event
are you running?
from async.
@WyriHaximus, Latest stable from pecl
Sockets support => enabled
Debug support => disabled
Extra functionality support including HTTP, DNS, and RPC => enabled
OpenSSL support => enabled
Thread safety support => disabled
Extension version => 3.0.6
libevent2 headers version => 2.0.21-stable
from async.
Related Issues (14)
- await() error in SimpleFiber.php HOT 17
- Fibers + Cancellation HOT 2
- Use 0.x versions HOT 5
- 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
- Basic question 'bout reactphp HOT 9
- 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.