uWSGI Perl support (PSGI)

PSGI is the equivalent of WSGI in the Perl world.

The PSGI plugin is officially supported and has an officially assigned uwsgi modifier, 5. So as usual, when you’re in the business of dispatching requests to Perl apps, set the modifier1 value to 5 in your web server configuration.

Compiling the PSGI plugin

You can build a PSGI-only uWSGI server using the supplied buildconf/psgi.ini file. Make sure thatthe ExtUtils::Embed module and its prerequisites are installed before building the PSGI plugin.

  1. python uwsgiconfig.py --build psgi
  2. # or compile it as a plugin...
  3. python uwsgiconfig.py --plugin plugins/psgi
  4. # and if you have not used the default configuration
  5. # to build the uWSGI core, you have to pass
  6. # the configuration name you used while doing that:
  7. python uwsgiconfig.py --plugin plugins/psgi core

or (as always) you can use the network installer:

  1. curl http://uwsgi.it/install | bash -s psgi /tmp/uwsgi

to have a single-file uwsgi binary with perl support in /tmp/uwsgi

Usage

There is only one option exported by the plugin: psgi <app>

You can simply load applications using

  1. ./uwsgi -s :3031 -M -p 4 --psgi myapp.psgi -m
  2. # or when compiled as a plugin,
  3. ./uwsgi --plugins psgi -s :3031 -M -p 4 --psgi myapp.psgi -m

Tested PSGI frameworks/applications

The following frameworks/apps have been tested with uWSGI:

Multi-app support

You can load multiple almost-isolated apps in the same uWSGI process using the mount option or using the UWSGI_SCRIPT/UWSGI_FILE request variables.

  1. [uwsgi]
  2.  
  3. mount = app1=foo1.pl
  4. mount = app2=foo2.psgi
  5. mount = app3=foo3.pl
  1. server {
  2. server_name example1.com;
  3. location / {
  4. include uwsgi_params;
  5. uwsgi_pass 127.0.0.1:3031;
  6. uwsgi_param UWSGI_APPID app1;
  7. uwsgi_param UWSGI_SCRIPT foo1.pl;
  8. uwsgi_modifier1 5;
  9. }
  10. }
  11.  
  12. server {
  13. server_name example2.com;
  14. location / {
  15. include uwsgi_params;
  16. uwsgi_pass 127.0.0.1:3031;
  17. uwsgi_param UWSGI_APPID app2;
  18. uwsgi_param UWSGI_SCRIPT foo2.psgi;
  19. uwsgi_modifier1 5;
  20. }
  21. }
  22.  
  23. server {
  24. server_name example3.com;
  25. location / {
  26. include uwsgi_params;
  27. uwsgi_pass 127.0.0.1:3031;
  28. uwsgi_param UWSGI_APPID app3;
  29. uwsgi_param UWSGI_SCRIPT foo3.pl;
  30. uwsgi_modifier1 5;
  31. }
  32. }

The auto reloader (from uWSGI 1.9.18)

The option –perl-auto-reload <n> allows you to instruct uWSGI to monitor every single module imported by the perl vm.

Whenever one of the module changes, the whole instance will be (gracefully) reloaded.

The monitor works by iterating over %INC after a request is served and the specified number of seconds (from the last run) is elapsed (this number of seconds is the value of the option)

This could look sub-optimal (you wil get the new content starting from from the following request) but it is the more solid (and safe) approach for the way perl works.

If you want to skip specific files from the monitoring, just add them with –perl-auto-reload-ignore

Remember that always modules in %INC are scanned, if you want to monitor your .psgi files, you need to specify them using the classic –touch-reload option

Notes

  • Async support should work out-of-the-box.
  • Threads are supported on ithreads-enabled perl builds. For each app, a new interpreter will be created for each thread. This shouldn’t be too different from a simple multi-process fork()-based subsystem.
  • There are currently no known memory leaks.

Real world example, HTML::Mason

  • Install the HTML::Mason PSGI handler from CPAN and create a directory for your site.
  1. cpan install HTML::Mason::PSGIHandler
  2. mkdir mason
  • Create mason/index.html:
  1. % my $noun = 'World';
  2. % my $ua = $r->headers_in;
  3. % foreach my $hh (keys %{$ua}) {
  4. <% $hh %><br/>
  5. % }
  6. Hello <% $noun %>!<br/>
  7. How are ya?<br/>
  8. Request <% $r->method %> <% $r->uri %><br/>
  • Create the PSGI file (mason.psgi):
  1. use HTML::Mason::PSGIHandler;
  2.  
  3. my $h = HTML::Mason::PSGIHandler->new(
  4. comp_root => "/Users/serena/uwsgi/mason", # required
  5. );
  6.  
  7. my $handler = sub {
  8. my $env = shift;
  9. $h->handle_psgi($env);
  10. };

Pay attention to comp_root, it must be an absolute path!

  • Now run uWSGI:
  1. ./uwsgi -s :3031 -M -p 8 --psgi mason.psgi -m
  • Then go to /index.html with your browser.