Here's my situation: I'm developing a web application using Dancer framework, and I would like to insert some data to the database on the server side from the browser side. The problem is, when the data is too large, the uploading takes so long that I'm considering displaying a progress bar describing the progress.

I implemented this by sending two requests: one for posting data, and the other polling the status. But it seems once the first requests is being handled, the other won't work until the first finishes. So the status returns nothing and suddenly 100%. To manage this, I create a thread when handling the first requests, so the main thread could return to handle the second polling requests. This works quite well until I have to kill some child progress spawned in the child thread (this is another question).

So my question is, is there any other ideas about dealing with the multiple requests simultaneously except for the multithread one? Normally how does the web programmers handle this situation?

有帮助吗?

解决方案

You should have no problem handling multiple requests simultaneously. How do you run your app? If you use built-in server (perl your_app.pl) then by default it is single threaded and will only process one request at a time.

You might want to use mutliprocess/multithread deployment options, for example Starman. It is described in https://metacpan.org/module/YANICK/Dancer-1.3113/lib/Dancer/Deployment.pod#Running-on-Perl-webservers-with-plackup

其他提示

You can use Dancer with plackup and Starman, here is a example:

foo.psgi:

#!/usr/bin/perl

use strict;
use warnings;
use Dancer2;

$| = 1;

get '/foo' => sub {
    `sleep 5`;
    'ok';
};

to_app;

Run the program with plackup:

$ plackup -s Starman foo.pl
Resolved [*]:5000 to [0.0.0.0]:5000, IPv4
Binding to TCP port 5000 on host 0.0.0.0 with IPv4
Setting gid to "0 0 0"
Starman: Accepting connections at http://*:5000/

Then run the following for loop:

for i in $(seq 1 3)
> do
>   time curl http://localhost:5000/foo &
> done

Output:

ok
real    0m5.077s
user    0m0.004s
sys 0m0.010s
ok
real    0m5.079s
user    0m0.001s
sys 0m0.012s
ok
real    0m5.097s
user    0m0.009s
sys 0m0.004s

You can see Dancer2 can accept multiple request now.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top