-
Notifications
You must be signed in to change notification settings - Fork 85
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
why is connect function always executed as last instruction? #69
Comments
I went into more depth to find out that the problem was in the send/close method. They are called synchronously, but the stream is passed to the server asynchronously and in my case they are all waiting for my procedure to complete (don't know why). The only way around was to execute the rest of my procedure in a callback called on $conn event "close". e.g.
Anyone of you found a better solution? |
ReactPHP works under the assumption that the loop must not be blocked. If you block the loop for this period of time, you're going to run into problems, see also https://github.com/reactphp/react/wiki/FAQ. This is a pretty common problem. As an alternative, I would suggest moving these kind of things off to a worker queue. |
The strange thing here is that I was expecting the ->send function to block the rest of the execution, instead what I got was a super fast ->send and ->close call, (that did nothing) and then the rest of my steps (4,5 which where just after the ->close call), and only then the ->send call would be really executed. Is it right? What I want to achieve is a batch script which informs the WS Server (and the clients) of its progression, so on each step I want to send a message to the server telling it "I've done step X". |
Both |
I'll try the child-process and let you know. My "progress" messages should be sent to the client which should reload a grid whenever some of the steps are completed, so I cannot use stdin/out. I'm not sure if there are best practices on how to handle these problems. |
Here's some pseudo code to get you started 👍 $process = new Process('ping google.com');
$process->start($loop);
$process->stdout->on('data', function ($chunk) use ($conn) {
$conn->send($chunk);
}); |
In addition to what Christian has said part of the reason nothing happens until the end of your procedure is that when using nonblocking I/O in PHP the code is lazy and doesn't execute until the event loop starts running, which if you're using the "simple" example from the README, happens at the end of the process. |
So in the end I should split my procedure into multiple Commands, at the end of which I send a websocket message and call php-cli process to execute the next step, right? |
|
Thanks guys! |
Hello,
I'm trying to send multiple WS messages during a long procedure (30 to 60 seconds).
Calls to connect function were put in different places but what I notice is that they are all executed as last instructions of my procedure, so what I get is a bunch of messages sent at the end of the procedure.
Is it supposed to work this way? Should I use Threads to avoid it?
Thanks!
The text was updated successfully, but these errors were encountered: