I want to write non-blocking applications. I use apache2, but I was reading about nginx and its advantage with respect to apache processes. I am considering changing out apache for nginx. My question is, is it possible to write non-blocking web applications with php and nginx?.
Or is a better idea to try and do this with python, using some reverse proxy like uwsgi or gunicorn with nginx? Or is the solution to learn nodejs?
Writing non blocking applications in php is possible, but it's probably not the best environment to do so, as it wasn't created keeping that in mind! You get a pretty decent control over your child processes using the process control library PCNTL but it obviously won't ever offer you same ease of use that other environments can give you!
I don't know python very well but personally I'd recommended you go with nodejs! It's a fairly new technology, that's true, but everything is non blocking there and it's meant to be that way! Basically what you have is a single thread (which you can extend however you want in this news versions) and literally everything (except you tell it to do differently) is going to be event-driven, leaving space to proceed on the process queue as expected!
Nodejs is really easy to learn, if you ever stumbled upon web applications, you know javascript anyways! it is still not hugely documented, but there are many ready to use modules you can download and use straight away!
As far as i known, there are several libraries available to build non-blocking applications.
BTW: i am the author of Skyray, and one of the developer of swoole.
I haven't looked at pushpin yet, but I'm writing this answer to point to another framework that supports non-blocking applications in PHP:
React http://reactphp.org/
It supports binding a server to HTTP, or WebSocket and can be used with webserver software such as Nginx by forwarding (using proxy_pass) requests to it over HTTP. You can even use HTTPS in the webserver and forward that non-encrypted. (SSL support in React is lacking.)
There doesn't appear to be FastCGI or UWSGI support yet, at least not in the upstream project.
The style is similar to writing node.js, you attach PHP 5.3+ closures to events, and it uses the non-blocking sockets transparently underneath. There are good examples on that website, so I won't repeat them here since you didn't ask for a use case specifically.
UPDATED: I just looked at the pushpin website and it's not a framework or tool for writing non-blocking PHP applications as requested in the question title. It does seem like it would be useful as part of a solution though.
Swoole is a good choice for writing non-blocking php application
here is a example:
$fp = stream_socket_client("tcp://127.0.0.1:80", $code, $msg, 3);
$http_request = "GET /index.html HTTP/1.1\r\n\r\n";
fwrite($fp, $http_request);
swoole_event_add($fp, function($fp){
echo fread($fp, 8192);
swoole_event_del($fp);
fclose($fp);
});
swoole_timer_after(2000, function() {
echo "2000ms timeout\n";
});
swoole_timer_tick(1000, function() {
echo "1000ms interval\n";
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With