What I know about the FCGI protocol is, the first time the application is called, it loads it into memory, run it, return the response to the server, finish the response but does not end the application, it keeps it running in memory, then next requests will use this compiled in memory copy of the application to process the request.
Reading about the PSGI protocol, it seems to be working the same way.
My question is, is my assumption correct, they are the same regarding the application speed to
requests per second.
the confusing issue also if they work the same, why I see plackup has command line option to enable FCGI.
You're asking for a comparison between apples and fruit. Your question doesn't make much sense.
There are various underlying mechanisms you can use to deploy a web application written in Perl.
It can be a standalone CGI program
It can run under mod_perl
It can run under FCGI
etc ...
The problem is that for each deployment mechanism you need to change the way that your program is written. This means that you need to know that you're, say, targetting mod_perl before you start writing the code. It also means that moving an application between these various deployment methods is non-trivial.
This is the problem that PSGI solves. Instead of writing a CGI app or a mod_perl app or a FCGI app, you write an app that targets the PSGI protocol. You can the deploy exactly the same app under CGI, mod_perl or FcGI (or many other deployment methods).
If you deploy your PSGI app using the FCGI handler, then it will work the same way as a FCGI app. But later on it's simple to move it to run as a mod_perl app. Or to run it as a standalone server using something like Starman.
Does that help at all?
Related
I'm writing a client-server application for OS X. The service needs to run forever, or at least as close as possible. :-)
In the past I have used "classic" Distributed Objects quite successfully with an Objective-C application, but this time I wanted to use Swift and the new shiny IPC technology, XPC!
So, here's my question:
When I create an XPC Mach Service (it needs root privileges) and start it via launchd, the process appears to restart for every new, incoming connection. I have written services started via launchd before and never had this problem. Is there something specific to using XPC that causes this?
My preference is to use a high-level IPC mechanism instead of something more fundamental like Unix domain sockets, but I'm happy to drop down to that level if necessary.
I am developing a prototype web application. I am using a toy http server which I have developed myself and it is written in C. The server needs to obtain text data from html form submitted and for furthur processing should pass it to a script which I have written in python. I know the following methods to achieve the same. Also once the processing is done the script has to return the data to server.
Save the data in a file and let the python script access it.
Calling the system or exec family of functions from within the Server code.
I have heard of CGI where a socket is used on both sides. I know how to do the same in my python script. But I am not able to see how the same is done in C server side.
Normally this is abstracted out as most people use apache or nginx as theor service. How can CGI be achieved in Server code?
CGI is covered here: https://www.rfc-editor.org/rfc/rfc3875
By 'CGI over sockets' you probably mean FastCGI which is a bit different (and involves a long running process listening on a Unix socket and sending back responses). Classical CGI involves spawning a short lived process which then receives the request parameters via environment variables (set this from your server via setenv().
Request data is sent to the spawned process via stdin (e.g. in Python, sys.stdin). The output response is then written (with headers and all) via stdout.
CGI is very simple and this is why it was so quick to be adopted by a high number of languages -- the learning curve to implementing CGI was rather quick.
FastCGI is similar to CGI only at the script language layer (i.e. the programming interface remains somewhat similar); however the actual mechanics are rather different (you need to serialize the information over Unix sockets).
I know this sound like a question for ServerFault but I know that developers often get the blame when servers are struggling so I thought a post here might be useful to people who still use Perl on the web.
THE STORY:
We had serious issues with defunct processes on our old Apache server so we decided to move to Apache 2. The new server performs much better, no denying that. Tests reveal however, that under a heavy load (~100 users per minute) defunct processes start quickly ramping up on the server and using SSH it is clear that these processes are using the CPU. To overcome these issues we decided to implement CGI::Fast which is a type of FastCGI in Perl. Having that in place the zombies are gone, however performance wise the server is not coping any better.
The results led me to assume that there isn't really a point implementing CGI::Fast if Apache 2 will efficiently reclaim the resources anyway.
Does any of you have come to a different conlusion?
In my opinion it’s not worth moving to anything but a PSGI/Plack-based solution in 2011, independent of any other details you mentioned.
The Plack ecosystem is so much greater than FastCGI’s. (advantage)
You can deploy to many more servers than FastCGI supports. (advantage)
You may run CGI scripts unmodified with e.g. Plack::App::CGIBin whereas CGI::Fast requires a rewrite. (advantage)
Rewriting a CGI program to conform to the PSGInterface requires slightly more effort than rewriting it to CGI::Fast. (disadvantage)
FastCGI is faster than plain CGI because Apache doesn't have to load perl for each new request. However, the scripts need to be reworked to remove the assumption that they are executed once for each request. A FastCGI script at its core typically represents some type of event loop, processing the requests as they come in.
You can use CGI::Fast for plain CGI scripts without reworking the script around an event loop, but you lose the "Fast" part of FastCGI this way, as perl still needs to be run once for every script.
FastCGI also only provides a large benefit if the biggest part of your CGI script is loading perl or executing one-time code. For many web applications, this is true. However, if your script needs to do a lot of work for each request, such that the overhead of loading perl is small, then you won't see a big performance benefit by using FastCGI.
CGI
It was too inefficient for anything but small sites. CGI spawns a new process for every incoming request to execute a script, a very resource intensive and inefficient way of doing things. No wonder it faded away over time as web applications became more complex.
FastCGI was introduced to avoid some of the issues with running languages inside the Apache process, as well as avoiding the inefficiency of CGI.
A FastCGI application is executed outside of the web server (Apache or other wise), and waits for requests from the web server using a socket. The web server and the FastCGI application can even be on separate physical machines and communicate over the network.
Because the web server adn the application processes are separate better isolation is possible.
There may be some situations where the compilation process takes more time than the program's run time. What should one do in those circumstances?
If we consider CGI scripts where it may be called hundreds or thousands of times every minute then the above problem may occur. How to avoid these problems? We can't skip the compilation process. How to deal with such situations?
If you are looking at Perl-based CGI scripts, consider using something like mod_perl or FastCGI, which address that exact issue (among others).
A more generic way of doing the same thing would be to build some sort of "server" application that loads once and listen for client connections. The clients can then be very small lightweight processes that just connect to the server and ask the server to perform whatever work is needed.
I wrote a Perl program which searches and manipulates one text file. This CGI process slurps the file directly into memory, manipulates the file based on the User's input, then generates the HTML result.
It works functional wise. However, I know that once I deploy on high volume server, it will not be able to respond in due time. I suspect the memory to be a bottleneck. What is the best way to share that file, such that it is read once into memory once the server starts and never again ?
The solution I am guessing is a server daemon which loads the file into memory and serves other processes/threads their data. If so, what is the best method to implement the IPC?
Use FastCGI. It effectively turns your CGI program into a little server that your web server calls. Its simple and works on lots of different web servers. Because your CGI runs in its own process it can run on a different machine from your web server, and you can scale your program across multiple application servers. And it works with most major programming languages.
The advantage of mod_perl is that it lets you poke at the guts of Apache using Perl. If you're not using that its overkill. mod_perl has a lot of drawbacks. It ties you to Apache and whatever version of Perl was compiled into mod_perl. It mixes up the configuration and memory space of all your Perl applications with Apache's which complicates configuration and leads to memory bloat. And mod_perl is really complicated and difficult to upgrade.
That's pretty much why mod_perl was created.