File: threaded.PL

package info (click to toggle)
libfcgi-perl 0.67-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 420 kB
  • ctags: 387
  • sloc: ansic: 3,299; makefile: 47; perl: 6
file content (52 lines) | stat: -rw-r--r-- 1,061 bytes parent folder | download | duplicates (25)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use Config;

open OUT, ">threaded.fpl";
print OUT "#!$Config{perlpath}\n";
print OUT while <DATA>;
close OUT;
chmod 0755, "threaded.fpl";
__END__

use FCGI;
use Thread;
use IO::Handle;

use constant THREAD_COUNT => 5;

sub doit {
    my $k = shift;
    my %env;
    my $in = new IO::Handle;
    my $out = new IO::Handle;
    my $err = new IO::Handle;

    my $request = FCGI::Request($in, $out, $err, \%env);

    while ($request->Accept() >= 0) {
	print $out
           "Content-type: text/html\r\n",
           "\r\n",
           "<title>FastCGI Hello! (multi-threaded perl, fcgiapp library)</title>",
           "<h1>FastCGI Hello! (multi-threaded perl, fcgiapp library)</h1>",
           "Request counts for ", THREAD_COUNT ," threads ",
	   "running on host <i>$env{SERVER_NAME}</i><P><CODE>";

	{
	    lock(@count);

	    ++$count[$k];

	    for(my $i = 0; $i < THREAD_COUNT; ++$i) {
		print $out $count[$i];
		print $out " ";
	    }
	}
	$request->Flush();
	sleep(1);
    }
}

for ($t = 1; $t < THREAD_COUNT; ++$t) {
    new Thread \&doit, $t;
}
doit(0);