Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: PSGI / Plack

by jdrago999 (Pilgrim)
on Aug 18, 2011 at 01:44 UTC ( #920830=note: print w/replies, xml ) Need Help??


in reply to PSGI / Plack

I use Plack/PSGI (ok, plackup and Plack::Request if we're being specific) when I want a tool to be persistent and to be available via http - but not necessarily running under my Apache httpd server.

Replies are listed 'Best First'.
Re^2: PSGI / Plack
by Logicus on Aug 18, 2011 at 01:46 UTC
    Do you mind if I ask why not under apache?

      Do you mind if I ask why not under apache?

      One example is a job queue server. Because you could run into a race condition where two simultaneous requests to the server fetch the same job from the queue, one solution is to have a single process server responding to requests for new jobs. Because there is only one process listening, you can guarantee that once a job is assigned to one client it will not be assigned to any other clients.

      If the job queue server ran under Apache, it might be more difficult to lock and synchronize the job queue while a job is assigned to a client requesting a job, because of Apache's multi-process/thread model.

      Another example is a wrapper to a program which can only handle one request at a time - like skype's dbus api. I used a Plack/PSGI app to host the code that actually makes the skype dbus api call. I don't expect the application to get *that* much load, but forcing everything through a single thread of execution in this case worked for me.

      NOTE: In the above examples I'm using plackup which allows me to run everything inside a single process. Starman for example is another PSGI server which is high performance and will let you run in a multi-thread/process model like you get with Apache httpd. Having the ability to choose between execution styles is very powerful - especially when it requires absolutely no change to your own code.

      Another reason to handle some jobs in an out-of-apache process (yet persistent) is for performance. Say I have to use some memory hog module or read in large files which causes my memory footprint to grow. I can't really do that in all of my apache processes because I might run out of RAM. Using a PSGI server like plackup or starman to host the memory hog in a persistent environment will give you better performance than executing the same code from a system(...) call, and you can also play with the maximum number of running threads your memory hog can use, further preventing memory problems. If I had to run everything in my Apache httpd processes I might quickly run out of RAM on web applications which deal with video encoding for example.

      Also if you find yourself in need of deploying updates to your code, a PSGI-based application which allows you to execute a few commands (handle the upload of new files, extract them into place, restart apache, etc) simply could not be done from within Apache. Not reliably anyway.

      Here is an example of such a simplistic standalone app: Re: Networking Perl — and some commentary/discussion: Need Help in Understanding Some Code - Map and Scalar Questions.

      When you run something like that with an engine like Starman (or Twiggy or plackup or…) you get a hot-restartable, hot-worker-assignable, little speed-demon with a tiny footprint that can be tested easily. Apache is deep and venerable but there are many ways to skin the HTTP cat. Many app developers today deploy on nginx or lighttpd instead for example. They do not have the feature bag apache does but they outperform it handily on several levels in simple webservice.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://920830]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2020-07-03 19:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?