Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Recommend an asynchronous HTTP server CPAN modules?

by sectokia (Sexton)
on Jun 05, 2020 at 23:43 UTC ( #11117744=perlquestion: print w/replies, xml ) Need Help??

sectokia has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

I am looking for module that provides a HTTP(S) server that supports spawning off a thread/fork for each connecting client, so that the processing for each can run simultenously.

One way seems to be: HTTP::Server::Simple with HTTP::Server::Simple::CGI::PreFork

Are there any other mature ones, maybe also based on NET::SERVER?

My application is that I generate HTML/CSS/JSON and other replies to GET/POST requests, the generating takes 10-20 seconds due to queries to other external databases etc. So even though this will be an internal app with low number of users, synchronously handling user requests results in poor user experience of having to wait/contend with each other.

Thanks!

  • Comment on Recommend an asynchronous HTTP server CPAN modules?

Replies are listed 'Best First'.
Re: Recommend an asynchronous HTTP server CPAN modules?
by Corion (Pope) on Jun 06, 2020 at 06:11 UTC

    You can also use Apache with mod_perl to do this.

    There also is Starman, which claims to be a high performance server.

Re: Recommend an asynchronous HTTP server CPAN modules?
by Your Mother (Bishop) on Jun 06, 2020 at 07:03 UTC

    Itís not Perl but I like uWSGI to run Perl webapps. Starman has recently received some fixes, in it or its deps, not sure, so it might be something worth looking at again. That said, uWSGI is used by a LOT of people, itís extremely flexible, and itís been basically bomb-proof for me for several years. You might need to wrap your code in a Plack layer but that is trivial if you know how and semi-trivial if you donít. More at Re: Most efficient way to run Perl scripts on nginx server?

Re: Recommend an asynchronous HTTP server CPAN modules?
by haukex (Bishop) on Jun 06, 2020 at 14:08 UTC

    I've been doing a lot of work with Mojolicious lately and I've been liking it. It provides Mojo::IOLoop::Subprocess to fork off a subprocess so that one doesn't block the server.

    #!/usr/bin/env perl use Mojolicious::Lite -signatures; use Mojo::IOLoop; get '/' => sub ($c) { $c->render(template => 'index'); } => 'index'; post '/doit' => sub ($c) { $c->render_later; Mojo::IOLoop->subprocess->run_p(sub { # this code is actually running in a subprocess! sleep 10; return "I did the thing!"; })->then(sub (@results) { $c->render(text => "@results"); })->catch(sub ($err) { $c->reply->exception($err); }); } => 'doit'; app->start; __DATA__ @@ index.html.ep % layout 'main', title => 'Hello, World!'; <div> %= form_for doit => ( method=>'post' ) => begin %= submit_button 'Do the thing' %= end </div> @@ layouts/main.html.ep <!DOCTYPE html> <html> <head><title><%= title %></title></head> <body> %= content </body> </html>

    You could combine this with e.g. aychnronous AJAX form submission like I showed here, or one could probably even send progress messages (a feature provided by Mojo::IOLoop::Subprocess) to the browser using something like Javascript's EventSource API, which I included an example of here.

Re: Recommend an asynchronous HTTP server CPAN modules?
by CountZero (Bishop) on Jun 06, 2020 at 08:09 UTC
    If you are not adverse to web-frameworks, such as Mojolicious, then Mojo::Server::Prefork may be of interest. The engine doing the work is Mojo::Server::Daemon which is a non-blocking HTTP server.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
      It is a shame that pre-fork does not work on Windows.
Re: Recommend an asynchronous HTTP server CPAN modules?
by hippo (Chancellor) on Jun 06, 2020 at 10:55 UTC

    I have recently been experimenting with Twiggy which is asynchronous by design. It has been surprisingly straightforward to use but although I have deployed it in some public-facing roles, none of those are high-volume or mission-critical so don't consider it battle tested by me.

    Are there any other mature ones

    Twiggy is mature in the sense that it's been around for a decade but hasn't seen a release in 5 years. Then again, it probably hasn't needed one. Given your description of the task it sounds like a good fit but YMMV, of course. Worth a look at least.

      Warning: Twiggy is asynchronous in the sense that it runs on top of an AnyEvent event loop. This means that all code in the request handlers has to be written in an asynchronous, callback style. If any part of that code ever blocks, the entire server becomes unresponsive. This is especially tricky if the request handlers have to do database operations; I only have direct experience with PostgreSQL, which has async support, but with severe limitations. For example, you have to implement connection pooling yourself if you ever want to serve more than one database operations at the same time.

      OTOH, I second the recommendation of Starman. By default, it works as a pre-forked server with a fixed number of workers (it even reaps and restarts its workers after they've served a number of requests), but it can be coaxed to adjust the number of workers between a configurable minimum and maximum in response to the server traffic (these options are not mentioned in Starman's docs, you have to dig down into Net::Server::PreFork, on which it is based on).

      Starman is also "easier" in the sense that if you have some simple, naive, synchronous server code that works with plackup's default server, it will work with Starman too with no changes.

Re: Recommend an asynchronous HTTP server CPAN modules?
by karlgoethebier (Abbot) on Jun 06, 2020 at 13:48 UTC

    See also

    ęThe Crux of the Biscuit is the ApostropheĽ

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11117744]
Approved by Paladin
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2020-10-24 15:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (245 votes). Check out past polls.

    Notices?