Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl Mojolicious - How to make it handle multiple connections at once?

I setup a quick Mojolicious server like this:

use Mojolicious::Lite;

get '/' => sub {
    my $self = shift;

    sleep 5; #sleep here, I'm testing multiple connections at once

    $self->render_text('Hello World!');
};

app->start;

I then start it with: perl Mojolicious.pl daemon --listen=https://127.0.0.1:3000

Problem is, if I run this command concurrently:

time curl https://127.0.0.1:3000/ -k

It seems to only use 1 thread for requests, because if I make multiple requests at once, they can take much longer than 5 seconds. It's as if they are all queued up.

Am I missing something here? I'm wanting to use Mojolicous, but only if it can handle more than one client at a time.

like image 307
jonathanpeppers Avatar asked Jul 20 '11 21:07

jonathanpeppers


3 Answers

mojo daemon is a standalone HTTP server meant for development use, not production, and it only runs a single thread. For production you would want to either use the fastcgi option and a FastCGI-supporting webserver, or install a nice PSGI-compatible server like Starman or Starlet or Plack::Handler::FCGI or Fastpass and then do

plackup -s Starman --port 3000 Mojolicious.pl
like image 78
hobbs Avatar answered Oct 21 '22 14:10

hobbs


I recommend Reading The Fine Manual of Mojolicious. The Guides are very important. Specifically the sections regarding Hypnotoad - the built-in pre-forking webserver.

like image 3
Mark Stinson Avatar answered Oct 21 '22 14:10

Mark Stinson


use AnyEvent;
use Mojolicious::Lite;

my @stack = ();

get '/' => sub {
    my $self = shift;

    $self->render_later;
    push @stack, AnyEvent->timer ( after => 5, cb => sub {
        $self->render_text('Hello World!');
    });
};

app->start;
like image 2
user1050755 Avatar answered Oct 21 '22 14:10

user1050755