Question

Scenario:

I would like to run non-blocking/asynchronous code on PHP, where the user makes a request and receives a 202 Accepted code and moves on to other tasks -- but PHP continues to execute. I've seen some solutions/hacks using ob_flush and Connection: close, but it seems newer browsers do not allow this and only show the final result. Besides, it doesn't seem like a proper solution.

I know PHP is by design a single-threaded language, and I don't want to say multithreaded PHP, but I believe there are solutions out there to do the job. I am currently thinking about using AMQP or STOMP (RabittMQ) to offset long running execution to other workers, although I feel PHP will still wait for the worker response (I am still studying it and it seems there is a one-directional approach to the message; forgive me for not having proper knowledge at this moment)

However... here are my actual questions:

I am tempted to try PHP V8js, but before I dwelve into a lot of configuration steps, I have three questions:

Do Javascript functions written in non-blocking fashion result in a non-blocking PHP script?

For example:

<?php

$v8 = new V8Js();

/* basic.js */
$JS = <<< EOT

setInterval(function(){print("Hello")},3000); 

EOT;

try {
  var_dump($v8->executeString($JS, 'basic.js'));
  echo "World";
  exit();
} catch (V8JsException $e) {
  var_dump($e);
}

?>

On the above code, "Hello" is set to be displayed 3 seconds later: does PHP wait for the full execution of the JS code and then echoes "World"? Or does it echo "World" and terminates the script -- but lets JS running? Another example could be an async ajax call on the JS code: would PHP wait for the full operation and then continue, or would it continue, terminate the script (to the user) but keep processing the JS code in background (of course, without being able to output anything to the user)?

Second question: is it OK to have PHP + V8 lib and Nodejs installed on the same machine, without conflicts?

And third: if the PHP + V8js solution is blocking/sync, what would you recommend as a non-blocking/async solution for PHP (if any)?

EDIT I've ran a quick test with V8js and I can tell the above code does not work at all. You receive a ReferenceError: setInterval is not defined error. It seems it is necessary to register extensions ( http://ca3.php.net/manual/en/v8js.registerextension.php ), but due to the poor documentation available, and the fact that Nodejs is far more advanced in terms of packages, I am not sure I'll keep trying to use V8js.

Still, I am looking for good async solutions for PHP. I'll post a separate, directed question, to this matter.

Was it helpful?

Solution

Firstly, the ReferenceError is because setInterval is really window.setInterval, a method within a browser's DOM.

V8 does not have, nor expose, a DOM. No DOM, no window object, therefore no window.setInterval() method.

As for V8Js, the V8Js extension waits until the execution of the script completes or times out. The latest version of the extension, which was broken (may be fixed now), now throws a V8JsTimeLimitException when a script has exceeded its limit. There is also a V8JsMemoryLimitException.

Pritam mentioned ReactPHP. That, or Ratchet, are good choices for what you're trying to do as well.

I also found this discussion interesting. It was from before the aforementioned timeout was released. Still a good read.

OTHER TIPS

Don't know about V8js but if you want to do Async prog. in PHP, have a look a ReactPHP

Adding as answer, because I can't comment (yet).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top