Replies: 8 comments 4 replies
-
That for loop blocks the event loop from doing anything until it has completed. |
Beta Was this translation helpful? Give feedback.
-
@renji566 Welcome to ReactPHP! I'm not entirely sure what you're trying to achieve, but it looks like you may want to use the $browser = new Browser();
try {
await($browser->get('http://127.0.0.1:8888/api.php/test/test3'));
for ($i = 0; $i < 50000; $i++) {
echo $i . PHP_EOL;
}
} catch (\Throwable $e) {
echo $e->getMessage();
} ReactPHP is designed to be async, so that any operations that may take a while (such as sending an HTTP request) will only start the operation and return a Promise to notify you when it is completed (or failed). While it is running ("in the background"), you may start any number of other async operations that would all run concurrently ("at the same time").
Given this is such a common requirement, we're currently working on ReactPHP v3 to make this even easier looking forward. In the future, we are planning to provide a I hope this helps! 👍 If so, consider supporting this project, for example by becoming a sponsor ❤️ |
Beta Was this translation helpful? Give feedback.
-
but in my try, i do not receive the request before the for loop success. i use the xdebug debug the requested url, it is also a php file
| |
***@***.***
|
|
***@***.***
|
…---- 回复的原邮件 ----
| 发件人 | Cees-Jan ***@***.***> |
| 日期 | 2025年01月22日 14:31 |
| 收件人 | ***@***.***> |
| 抄送至 | ***@***.***>***@***.***> |
| 主题 | Re: [reactphp/reactphp] the async request do not be sent before the whole script execute over? (Discussion #598) |
When you execute that code it will start opening the connection. It has to wait on the server before it can continue, and that's the moment you get a promise returned and your for loop kicks in. Once that is completed the rest will run and go through the full request/response cycle.
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you authored the thread.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
I want the request to be send immediately, but I'm debugging with xdebug and I don't get the request until the loop ends
| |
***@***.***
|
|
***@***.***
|
…---- 回复的原邮件 ----
| 发件人 | Christian ***@***.***> |
| 日期 | 2025年01月22日 16:15 |
| 收件人 | ***@***.***> |
| 抄送至 | ***@***.***>***@***.***> |
| 主题 | Re: [reactphp/reactphp] the async request do not be sent before the whole script execute over? (Discussion #598) |
@renji566 Welcome to ReactPHP!
I'm not entirely sure what you're trying to achieve, but it looks like you may want to use the await() function (or promise handlers alternatively):
$browser = newBrowser();
try {
await($browser->get('http://127.0.0.1:8888/api.php/test/test3'));
for ($i = 0; $i < 50000; $i++) {
echo$i . PHP_EOL;
}
} catch (\Throwable$e) {
echo$e->getMessage();
}
ReactPHP is designed to be async, so that any operations that may take a while (such as sending an HTTP request) will only start the operation and return a Promise to notify you when it is completed (or failed). While it is running ("in the background"), you may start any number of other async operations that would all run concurrently ("at the same time").
If you want to use the result, you likely want to "await" its resolution, so in most cases you would simply use the await() function as suggested above.
As a less straight-forward alternative, you may also just "wait" some amount of time until the operation is completed. In your case, this would be at the end of program, but you may also sprinkle some delay(0.001) calls in your for loop to give it some time resolve somewhere "in the middle" of your execution.
Given this is such a common requirement, we're currently working on ReactPHP v3 to make this even easier looking forward. In the future, we are planning to provide a get() method that would automatically await the result and a getAsync() method that would return a promise for async execution. You can learn more about our plans here: #481
I hope this helps! 👍 If so, consider supporting this project, for example by becoming a sponsor ❤️
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
The loop has no meaning, it just block the whole script. What I meant was that I wanted the request to be send immediately, but in reality, I didn't detect the request being sent before the loop ended
…---- Replied Message ----
| From | Cees-Jan ***@***.***> |
| Date | 01/24/2025 03:12 |
| To | ***@***.***> |
| Cc | ***@***.***>***@***.***> |
| Subject | Re: [reactphp/reactphp] the async request do not be sent before the whole script execute over? (Discussion #598) |
What does the loop represent because this feels like you took out something else and put in the loop to show off your issue.
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
i do not need the response,so i do not wait .
What I meant was that I wanted the request to be send immediately, but in reality, I didn't detect the request being sent before the loop ended
…---- Replied Message ----
| From | Christian ***@***.***> |
| Date | 01/23/2025 21:49 |
| To | ***@***.***> |
| Cc | ***@***.***>***@***.***> |
| Subject | Re: [reactphp/reactphp] the async request do not be sent before the whole script execute over? (Discussion #598) |
What you're describing is only possible if you're missing the await() function, double-check you're using the await() function as suggested above 👍
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
@renji566 is it possible that you didn't notice that the event loop is started when the php script shuts down? Because executing your current code would mean the following: <?php
require __DIR__ . '/vendor/autoload.php';
$client = new \React\Http\Browser();
$url = 'https://github.com/joomla/joomla-cms/releases/download/5.2.3/Joomla_5.2.3-Stable-Full_Package.zip';
$client->get($url)->then(function (\Psr\Http\Message\ResponseInterface $response) {
var_dump($response->getHeaders(), (string)$response->getBody());
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
for ($i = 0; $i < 50000; $i++) {
echo $i .'-';
} This code would mean you "register" the get request, but this get request will be run when the event loop has time for it. In this case the loop hasn't been started yet. It's the same as if you would run this variant: <?php
require __DIR__ . '/vendor/autoload.php';
$client = new \React\Http\Browser();
$url = 'https://github.com/joomla/joomla-cms/releases/download/5.2.3/Joomla_5.2.3-Stable-Full_Package.zip';
$client->get($url)->then(function (\Psr\Http\Message\ResponseInterface $response) {
var_dump($response->getHeaders(), (string)$response->getBody());
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
for ($i = 0; $i < 50000; $i++) {
echo $i .'-';
}
// that actually happens when your entry file has no further source code to execute
$loop = \React\EventLoop\Loop::get();
$loop->run(); Now if you move the eventloop run before your loop it will do what you expect. <?php
require __DIR__ . '/vendor/autoload.php';
$client = new \React\Http\Browser();
$url = 'https://github.com/joomla/joomla-cms/releases/download/5.2.3/Joomla_5.2.3-Stable-Full_Package.zip';
$client->get($url)->then(function (\Psr\Http\Message\ResponseInterface $response) {
var_dump($response->getHeaders(), (string)$response->getBody());
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
// that actually happens when your entry file has no further source code to execute
$loop = \React\EventLoop\Loop::get();
$loop->run();
for ($i = 0; $i < 50000; $i++) {
echo $i .'-';
} But the important thing what you might miss, reactphp (and all other event loop frameworks) still execute codes only in one thread (no parallel execution). Except curl and other libraries which handles execution out of the php context, like threads, forks or kernel space). So in case you run cpu intensive tasks in your loop you must allow php to run other code. How exactly this is done is in my opinion not so "good" or easy documented, read the promises readme as start. And sorry if you know this all already ;-) |
Beta Was this translation helpful? Give feedback.
-
think you sir
…---- Replied Message ----
| From | Harald ***@***.***> |
| Date | 01/25/2025 22:07 |
| To | ***@***.***> |
| Cc | ***@***.***>***@***.***> |
| Subject | Re: [reactphp/reactphp] the async request do not be sent before the whole script execute over? (Discussion #598) |
@renji566 is it possible that you didn't notice that the event loop is started when the php script shuts down?
Because executing your current code would mean the following:
<?phprequire__DIR__ . '/vendor/autoload.php';
$client = new \React\Http\Browser();
$url = 'https://github.com/joomla/joomla-cms/releases/download/5.2.3/Joomla_5.2.3-Stable-Full_Package.zip';
$client->get($url)->then(function (\Psr\Http\Message\ResponseInterface$response) {
var_dump($response->getHeaders(), (string)$response->getBody());
}, function (Exception$e) {
echo'Error: ' . $e->getMessage() . PHP_EOL;
});
for ($i = 0; $i < 50000; $i++) {
echo$i .'-';
}
This code would mean you "register" the get request, but this get request will be run when the event loop has time for it. In this case the loop hasn't been started yet.
It's the same as if you would run this variant:
<?phprequire__DIR__ . '/vendor/autoload.php';
$client = new \React\Http\Browser();
$url = 'https://github.com/joomla/joomla-cms/releases/download/5.2.3/Joomla_5.2.3-Stable-Full_Package.zip';
$client->get($url)->then(function (\Psr\Http\Message\ResponseInterface$response) {
var_dump($response->getHeaders(), (string)$response->getBody());
}, function (Exception$e) {
echo'Error: ' . $e->getMessage() . PHP_EOL;
});
for ($i = 0; $i < 50000; $i++) {
echo$i .'-';
}
// that actually happens when your entry file has no further source code to execute$loop = \React\EventLoop\Loop::get();
$loop->run();
Now if you move the eventloop run before your loop it will do what you expect.
<?phprequire__DIR__ . '/vendor/autoload.php';
$client = new \React\Http\Browser();
$url = 'https://github.com/joomla/joomla-cms/releases/download/5.2.3/Joomla_5.2.3-Stable-Full_Package.zip';
$client->get($url)->then(function (\Psr\Http\Message\ResponseInterface$response) {
var_dump($response->getHeaders(), (string)$response->getBody());
}, function (Exception$e) {
echo'Error: ' . $e->getMessage() . PHP_EOL;
});
// that actually happens when your entry file has no further source code to execute$loop = \React\EventLoop\Loop::get();
$loop->run();
for ($i = 0; $i < 50000; $i++) {
echo$i .'-';
}
But the important thing what you might miss, reactphp (and all other event loop frameworks) still execute codes only in one thread (no parallel execution). Except curl and other libraries which handles execution out of the php context, like threads, forks or kernel space).
So in case you run cpu intensive tasks in your loop you must allow php to run other code. How exactly this is done is in my opinion not so "good" or easy documented, read the promises readme as start.
And sorry if you know this all already ;-)
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
$browser = new Browser(); try { $browser->get('http://127.0.0.1:8888/api.php/test/test3'); for ($i = 0; $i < 50000; $i++) { echo $i . PHP_EOL; } } catch (\Throwable $e) { echo $e->getMessage(); }
like before code , the request do not be send before the for loop success, or it is the windows system limit?
Beta Was this translation helpful? Give feedback.
All reactions