How to allow multiple connections to Dancer2

2019-09-14 08:04发布

问题:

I am using Perl Dancer2 as the RESTful service framework with the basic setting (using command

dancer2 -a MyWeb::App

to generate template files and add "get" routes in the auto-generated MyWeb-App/lib/MyWeb/App.pm file). Recently I found out that when one request needs a long period of time to finish, the server is locked to only serve that request. For example

get '/' => sub {# simple request to redirect to a static page
    template 'index'; #template directive Templates all go into the views/
};
get '/compute' => sub{
    for (my $i=0;$i<1000000;$i++){
         wait(1000);  #simulate long computation time 
    }
    return "Done!";
};

When firstly in one tab http://myhost.com/compute is entered, in another tab the link http://myhost.com/ won't display anything until the previous /compute route finishes, which seems to me that only one connection is allowed at a time. The question is how to set up the Dancer2 server to allow multiple connections, i.e. the two tabs mentioned above can be run at the same time?

Many thanks!

回答1:

How are you deploying your Dancer2 app? If you're using plackup, then you should realise that the default server that plackup uses only supports a single connection. But you can use the -s option to change to something like Starman, which supports multiple connections. See Dancer2::Manual::Deployment for more details.

If that's not the case, then you need to tell us more about your application. Is it possible that requests are blocking because they all need access to some shared resource?



标签: perl dancer