Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

\React\Async\async did not invokes in on('data') event #85

Closed
Carsak opened this issue Dec 1, 2023 · 2 comments
Closed

\React\Async\async did not invokes in on('data') event #85

Carsak opened this issue Dec 1, 2023 · 2 comments
Labels
question Further information is requested

Comments

@Carsak
Copy link

Carsak commented Dec 1, 2023

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?

@Carsak Carsak added the bug Something isn't working label Dec 1, 2023
@Carsak Carsak changed the title \React\Async\async did not invokes in on('data) event \React\Async\async did not invokes in on('data') event Dec 1, 2023
@SimonFrings
Copy link
Member

Hey @Carsak, you're currently using a blocking function (file_put_contents()) inside your async function. The async() function only works in tandem with the await() function and does not "magically" make any blocking function non-blocking. This is also described in the documentation for the async() function. So as long as you're not using the await() function, it doesn't make much sense to use async() here.

I believe this should answer your question, so I will close this ticket for now. We can reopen this if you encounter the same issues again after trying out the solutions mentioned in here. If you do need to reopen the ticket, please provide some information about the steps you've taken so far, what worked and what didn't.

FYI: I think this kind of ticket is better suited for a discussion in our "Q&A" section. We recommend opening bug tickets only when you can provide evidence of something being broken, just as a friendly reminder for future tickets :)

@SimonFrings SimonFrings added question Further information is requested and removed bug Something isn't working labels Dec 15, 2023
@Carsak
Copy link
Author

Carsak commented Dec 15, 2023

thank you @SimonFrings

I solved my problem in another way. Maybe more radical, but it works.
Details in https://github.com/orgs/reactphp/discussions/550

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants