8000 Allow waiting inside a loop by ticking instead of running the loop by JurJean · Pull Request #19 · clue/reactphp-block · GitHub
[go: up one dir, main page]

Skip to content

Allow waiting inside a loop by ticking instead of running the loop #19

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

Closed
wants to merge 1 commit into from
Closed

Allow waiting inside a loop by ticking instead of running the loop #19

wants to merge 1 commit into from

Conversation

JurJean
Copy link
@JurJean JurJean commented May 25, 2016

I needed to wait for a promise while already within a loop. Because the await() function calls $loop->stop(), the loop I was already in stopped.

I removed $loop->stop() and instead of $loop->run() I now call $loop->tick(). This has resolved my problem. The unittests are still working as well.

Please let me know about any problems with this approach, I will try to resolve them.

@clue
Copy link
Owner
clue commented May 25, 2016

Please let me know about any problems with this approach, I will try to resolve them.

Thanks for your PR @JurJean! 👍

This change essentially reverts #1: Running tick() within the loop is essentially busy-waiting, i.e. this will very likely take 100% CPU usage until the promise resolves.

I needed to wait for a promise while already within a loop.

I'm not sure this is something we ought to support. Can you give some use cases / usage examples?

@JurJean
Copy link
Author
JurJean commented May 25, 2016

That's sad to hear! The reason I want to do this is that I don't like everything becoming a Closure while using react, and want to create solid code where I can. The thing is, that I'm using the same loop for both serving http requests and fetching data from eventstore over tcp (just for the fun of it). The loading of events can be refactored to be asynchronous I think, but the writing cannot. I tried to create two loops but that did not work, the http loop stopped working. Maybe I need to rethink the whole thing, but still, I feel it should be possible. Here are some points of interest of the application I'm talking about:
https://github.com/mhwk/spray-cqrs-server/blob/improvement/yielding/bin/server.php
https://github.com/mhwk/spray-cqrs-server/blob/improvement/yielding/src/GetEventStore/Store.php#L67
https://github.com/mhwk/spray-cqrs-server/blob/improvement/yielding/src/GetEventStore/StoreMutationStream.php#L112

Furthermore, I can do it with Akka framework in Java. While they say it's discouraged to do, you may do so if you know you have to.
http://doc.akka.io/docs/akka/snapshot/java/futures.html#Use_with_Actors

@JurJean
Copy link
Author
JurJean commented May 27, 2016

I've created a pull request at react/event-loop that resolves the cpu usage problem calling tick() in a while loop.

reactphp/event-loop#52

@clue
Copy link
Owner
clue commented Aug 12, 2016

I don't like everything becoming a Closure while using react, and want to create solid code where I can

I'm not sure I follow that sentiment, as it's very well possible (and my personal aim 8000 ) to write SOLID code when using React PHP.

The loading of events can be refactored to be asynchronous I think, but the writing cannot. I tried to create two loops but that did not work […]

I think this is the real culprit here. React is built around the concept of using a single EventLoop (the reactor) that will block everything else. I'm not familiar with the domain you're in, but it usually should be possible to rewrite your "writing process" to be async. This should allow you to use a single loop and fix any problems you're seeing 👍

I hope this helps 👍

@clue clue closed this Aug 12, 2016
@JurJean
Copy link
Author
JurJean commented Aug 12, 2016

Thanks for your response! I'm sure stuff can be refactored into more solid code. Fact remains, that it's perfectly acceptable to wait for something to become available, without the use of a callback. In other languages its done all the time.

I'm not talking about running a second event loop, I want to keep running the loop non-blocking, until some result becomes available and then continue.

I really don't see what would be the problem as it's implemented with a few changes in the code. Currently using icicleio, which does support it, and its awesome.

@clue
Copy link
Owner
clue commented Aug 12, 2016

Fact remains, that it's perfectly acceptable to wait for something to become available, without the use of a callback.

I'm still not sure I follow that sentiment :-) This library is here to do exactly this, it allows you to await something without using callbacks and hence has to block the loop.

I'm not talking about running a second event loop, I want to keep running the loop non-blocking, until some result becomes available and then continue.

React is built around the concept of using only non-blocking code or using IPC for blocking parts, but not ever mixing these two. I understand where you're coming from, as this often makes integration with existing (traditional) systems more complex and this library is here to help :-)

I really don't see what would be the problem as it's implemented with a few changes in the code.

The suggested PR builds on the assumption that tick() does block. This is not currently guaranteed and I'm unsure this is something that is going to land any time soon. There's been some discussion as to whether the tick() method should be removed altogether, because it leaks the implementation detail of "ticks".

Currently using icicleio, which does support it, and its awesome.

Happy to hear you've got this sorted 👍

@JurJean
Copy link
Author
JurJean commented Aug 12, 2016

Allright then, still not convinced but I'm sure you know you're talking about :) Thanks anyway!

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

Successfully merging this pull request may close these issues.

2 participants
0