reactphp / async Goto Github PK
View Code? Open in Web Editor NEWAsync utilities and fibers for ReactPHP.
Home Page: https://reactphp.org/async/
License: MIT License
Async utilities and fibers for ReactPHP.
Home Page: https://reactphp.org/async/
License: MIT License
We should support accepting an iterable
instead of only an array
for parallel()
, series()
and waterfall()
. The iterable
pseudo type is available as of PHP 7.1, so we can take advantage of this for our Async v3 and Async v4 (see also #11 and #14). It supports passing an array
just like now, but also accepts Iterator
s and Generator
s.
I'm not currently working on this, but figured it makes sense to report here in order to track this feature request. In case anybody feels like picking this up, any input and PRs would be much appreciated ๐ (I've also filed the same suggestion for Promise v3 in reactphp/promise#221)
Hi everybody,
<?php
require __DIR__ . '/async/vendor/autoload.php';
use React\EventLoop\Loop;
use React\Promise\Promise;
use function React\Async\await;
use function React\Async\async;
Loop::addTimer(0.5, React\Async\async(function() {
echo 'a';
React\async\await(React\Promise\Timer\sleep(1.0));
echo 'c'; // not echoed?
}));
Loop::addTimer(1.0, fn() => echo 'b'); // line 16
I am already struggling with the simplest example (copied from the main page).
I am using PHP 8.1.2 on Win10 with the newest async.
Thank you!
(Great job with Fibers initiative!)
For test purposes I'm refactoring my app with await() function in place of promises-chains. I've got an error:
2021-11-29 21:56:23 [EXCEPTION] Error: Value of type null is not callable in ...\vendor\react\async\src\SimpleFiber.php:59
2021-11-29 21:56:23 [EXCEPTION] #0 ...\vendor\react\async\src\functions.php(92): React\Async\SimpleFiber->suspend()
2021-11-29 21:56:23 [EXCEPTION] #1 ...\MessagesProcessor.php(114): React\Async\await(Object(React\Promise\Promise))
It occurs when I try to use async() function. It's hard to say how to reproduce this bug. In the same call stack, a few promises before, I use a React\Http\Browser with async() function. If I comment this usage (replace to something like resolve(new ResultObject())
) - error doesn't occur. If I use Browser with promise-style api, the PHP process ends with error:
Process finished with exit code -1073741819 (0xC0000005)
I'm using the newest versions of all packages, ofc PHP 8.1.
Do you know what can cause these errors?
We should support Promise v3 for Async v2, v3 and v4. I don't think implementing this here requires a lot of work, the only outstanding feature that would be debatable is cancellation semantics as discussed in #42.
I'm not currently working on this, but figured it makes sense to report here in order to track this feature request. In case anybody feels like picking this up, any input and PRs would be much appreciated ๐
๐ I've ran into an issue with using await
in a destructor. The error triggered is Fatal error: Uncaught FiberError: Cannot switch fibers in current execution context in SimpleFiber.php on line 66
.
index.php
<?php
declare(strict_types=1);
use function React\Async\await;
require_once __DIR__ . '/vendor/autoload.php';
$foo = new class {
public function __construct()
{
await(\React\Promise\Timer\sleep(1));
print "construct\n";
}
public function __destruct()
{
await(\React\Promise\Timer\sleep(1));
print "destruct\n";
}
};
Using the 4.x
branch; the issue seems to exit with both StreamSelectLoop
and ExtUvLoop
.
What should the following code return?
$promise = async(static function (): int {
try {
await(React\Promise\Timer\sleep(2));
} catch (Exception $e) {
return 1;
}
return 2;
})();
$promise->cancel();
var_dump(await($promise));
At the moment, the code would print 1
. The same cancellation logic is triggered for fibers (#20) and coroutines (#13). Accordingly, this affects Async v3 and Async v4.
Perhaps we should reject the entire promise? (See also reactphp/promise#56 for discussion about cancellation semantics in Promise v3).
Hi,
Sorry, for this - again - basic question about reactphp
To make my question more clear, I use this DNS lookup example:
<?php
$result = [];
$domains = ['domain1.com', 'domain2.xyz'];
foreach ($domains as $domain) {
$result[] = dns_get_record($domain);
}
var_dump($result);
Can above example executed in parallel (to make it faster) with reactphp or not (and if yes, how with async? or event-loop? can you give me an additional hint)?
If I understand this stackoverflow post correctly, it can not (but I do not think so).
Thank you again.
Hello.
The following code is causing an error:
EventBase::loop(): event_base_loop: reentrant invocation. Only one event_base_loop can run on each event_base at once.
<?php
use React\EventLoop\Loop;
use React\Promise\Deferred;
use function React\Async\await;
require __DIR__ . '/vendor/autoload.php';
Loop::addTimer(0, function () {
$deferred = new Deferred();
Loop::addTimer(0, static fn() => $deferred->resolve());
await($deferred->promise());
});
Tested on ExtEventLoop.
Reproduce:
use React\EventLoop\Factory as Loop;
use React\Async;
use React\Promise\Deferred;
$loop = Loop::create();
$deferred = new Deferred;
$promise = $deferred->promise();
$loop->addTimer(0.02, function() use($deferred) {
$deferred->resolve();
});
$foo = function() use($promise) {
Async\await($promise);
echo 'All good' . PHP_EOL;
};
$loop->addTimer(0.01, $foo);
$loop->run();
How to fix it is known: wrap $foo
into Async\async
. But the error sucks anyway. You guys gotta find a way to get it either work anyway, or throw a nice error telling us what is wrong and hinting what to do.
Hello there,
Now that react/async is open to public, would you consider using 0.x.x versions until 1.0 is not finalized instead of relying on commit hashes?
It took me a while after realizing an important BC break introduced by 4355fcf after a composer update ๐ค
With a semver-like approach, BC breaks would be easier to spot. WDYT?
I have TCP client
$connector = new \React\Socket\Connector();
that connect to server and listen.
I add handler to 'data' event.
// did not write to event.log
$connector = new \React\Socket\Connector();
$connector->connect($host)->then(function (\React\Socket\ConnectionInterface $connection) {
$connection->on('data', \React\Async\async(
function ($data) {
file_put_contents('/home/carsak/event.log', $data . PHP_EOL, FILE_APPEND);
}
));
});
function inside async did not invoked
but if I wrote without async function, function write logs into event.log file
// it works
$connector = new \React\Socket\Connector();
$connector->connect($host)->then(function (\React\Socket\ConnectionInterface $connection) {
$connection->on('data',
function ($data) {
file_put_contents('/home/bitrix/dialer/event.log', $data . PHP_EOL, FILE_APPEND);
}
);
});
I checked docs several times and read about
The async() function is specifically designed for cases where it is used as a callback (such as an event loop timer, event listener, or promise callback). For this reason, it returns a new function wrapping the given $function instead of directly invoking it and returning its value.
Follow the example
use function React\Async\async;
Loop::addTimer(1.0, async(function () { โฆ }));
$connection->on('close', async(function () { โฆ }));
$stream->on('data', async(function ($data) { โฆ }));
$promise->then(async(function (int $result) { โฆ }));
My composer.json
{
"require": {
"react/socket": "1.12",
"react/event-loop": "1.3",
"ext-pdo": "*",
"ext-curl": "*",
"monolog/monolog": "^2.9",
"elasticsearch/elasticsearch": "^8.8",
"react/async": "^4.2"
},
"config": {
"allow-plugins": {
"php-http/discovery": true
}
}
}
What I did wrong?
What was here is no longer relevant.
Hello,
The code below doesn't work as expected.
At the end, strlen()
should return a non-zero.
Am I missing something maybe?
<?php
require __DIR__ . '/vendor/autoload.php';
React\Async\async(function (): void {
$browser = new React\Http\Browser();
$response = $browser->requestStreaming(
'GET',
'https://file-examples-com.github.io/uploads/2017/04/file_example_MP4_1920_18MG.mp4',
[
'Range' => 'bytes=0-499',
],
);
/** @var Psr\Http\Message\ResponseInterface */
$response = React\Async\await($response);
/** @var React\Stream\ReadableStreamInterface */
$body = $response->getBody();
$body = React\Promise\Stream\buffer($body);
$body = React\Async\await($body);
echo strlen($body), "\n"; // 0
})();
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.