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

infinite loop #15

Closed
yonus opened this issue Jan 12, 2016 · 14 comments
Closed

infinite loop #15

yonus opened this issue Jan 12, 2016 · 14 comments
Labels

Comments

@yonus
Copy link

yonus commented Jan 12, 2016

hello ,

I use guzzle library , react promise , block react. my problem is that the system loops infinitely when I call as following

       $loop = React\EventLoop\Factory::create();
        $requestPromise = $this->sendMessageTravelportApi($requestXml, $endpoint);
        $responseXml = Clue\React\Block\await($requestPromise, $loop);

the requestPromise as following:

 $requestDeferred = new React\Promise\Deferred();
        $requestParams = new Request("POST", $endpoint, $header, $message);
        $client = new Client();
        $client->sendAsync($requestParams)->then(
                function (ResponseInterface $res) use($requestDeferred) {
            // $res->getStatusCode() . "\n";
            $requestDeferred->resolve($res->getBody()->getContents());
        }, function (RequestException $e) use($requestDeferred) {
            $requestDeferred->reject();
        });
        return $requestDeferred->promise();

So I cant find why system loops infinitely.

@clue clue added the question label Mar 9, 2016
@clue
Copy link
Owner

clue commented Mar 9, 2016

The await() function runs the event loop until the promise is resolved – whereas in your case the loop is not actually responsible for resolving the promise. I'm not familiar with your Client instance, but you'd need one that binds to the event loop (e.g. the clue/buzz-react package mentioned in the README).

You may also want to look into using v1.1 of this lib so that you can at least add a timeout to your (failing) function (#14).

I hope this helps 👍

@clue clue closed this as completed Mar 9, 2016
@edolix
Copy link

edolix commented Apr 16, 2019

@clue if you try the \Block\await() block with a non-block Promise like the example from @yonus (ag: based on a network request) you'll get an infinite loop caused by this while loop: https://github.com/clue/reactphp-block/blob/master/src/functions.php#L83-L85

If $wait is true, the while loop will block the process and the Promise will never be resolved/rejected.

Other thing on $loop->run();:

Likewise, it is imperative to ensure the application actually invokes this method once. Adding listeners to the loop and missing to actually run it will result in the application exiting without actually waiting for any of the attached listeners.

This method MUST NOT be called while the loop is already running. This method MAY be called more than once after it has explicity been stop()ped or after it automatically stopped because it previously did no longer have anything to do.

From: https://reactphp.org/event-loop/#run.

@clue
Copy link
Owner

clue commented Apr 17, 2019

@edolix Can you be more specific in what exactly will block and/or provide a small gist to reproduce this?

I'm well aware of how the EventLoop works. I wrote the documentation you're quoting.

I think we agree that this project is not how most people should be using the EventLoop. That being said, it solves some use cases, including integrating a non-blocking HTTP client in a blocking execution environment as described in https://github.com/clue/reactphp-block#quickstart-example

@edolix
Copy link

edolix commented Apr 17, 2019

@clue take a look at this small App: it's faster then split all the files in gist.

php-await-test.zip

If you run the index.php file you'll see the logs.. the first example using Promise works, the second one using await will loop infinitely (ref: src/Connection.php L57 - 77)

@clue
Copy link
Owner

clue commented Apr 17, 2019

@edolix Your example seems to use two independent EventLoop interfaces. See https://github.com/ratchetphp/Pawl#example on how to explicitly pass the EventLoop to Pawl. If you're using the same EventLoop for attaching and waiting, this should work just fine 👍

@edolix
Copy link

edolix commented Apr 17, 2019

@clue yep, but the main Pawel EventLoop is for the websocket connection that can't be stopped by your await function here: https://github.com/clue/reactphp-block/blob/master/src/functions.php#L69

@clue
Copy link
Owner

clue commented Apr 17, 2019

ReactPHP has no concept of a "main" EventLoop. It is your responsibility to create an EventLoop instance once and then pass it around to your whole application.

Pawl automatically uses a new loop internally if you don't pass one. This works just fine if this one connection is the only thing your want to do in your application. If you want to do anything else in your application, then you have to explicitly pass this loop instance so that it will be shared in your whole application. This should work just fine if you follow the last link 👍

@edolix
Copy link

edolix commented Apr 17, 2019

It works but Pawel's EventLoop will be stopped by the first await call... The result is the websocket connection will be closed.
That's the problem using the same EventLoop instance with your await.

@clue
Copy link
Owner

clue commented Apr 17, 2019

That's what I'm saying. You MUST use the same EventLoop for Pawl and your await call. See https://github.com/ratchetphp/Pawl#example for details if you want to pass an EventLoop to Pawl.

@edolix
Copy link

edolix commented Apr 17, 2019

@clue i already tried and the problem is the WebSocket connection will be closed by

$loop->stop();

App to reproduce with the same EventLoop to Pawel and await.
php-await-test-2.zip

Right after the second test... the script will exit.

@edolix
Copy link

edolix commented Apr 17, 2019

To better explain my main goal: have a long-live PHP script connected to a WebSocket and use this library where i need to await responses from the server (and then work with the response, not close the EventLoop).

@edolix
Copy link

edolix commented Apr 18, 2019

@clue any thoughts or tips?

@clue
Copy link
Owner

clue commented Apr 18, 2019

@edolix It sounds like you may want to avoid blocking (awaiting) the script in the first place. I would suggest using a more event-driven approach and registering an event handler to the message event. This way, you wouldn't want to use this project here in your application. I hope this helps 👍

@edolix
Copy link

edolix commented Apr 18, 2019

Yep, thank you! 👍

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

No branches or pull requests

3 participants