I use following code:
#!/usr/bin/env perl
use v5.16.3;
use threads (exit => 'threads_only');
use threads::shared;
use IO::Select();
use FCGI;
my $listen = ':5000';
my $socket = FCGI::OpenSocket($listen, 100);
$SIG{INT} = sub {
map { $_->kill('TERM') } threads->list(threads::all);
};
threads->create(\&thread, $socket);
threads->create(\&thread, $socket);
threads->create(\&thread, $socket);
while (threads->list(threads::all)) { sleep 1 } #wait until all threads
finished
sub thread {
my $socket = shift;
$SIG{TERM} = sub {
threads->detach;
threads->exit;
};
my $env = {};
my $req = FCGI::Request(\*STDIN, \*STDOUT, \*STDERR, $env, $socket, 1);
my $sel = IO::Select->new($socket);
while (1) {
if ($sel->can_read(1) && $req->Accept() >= 0) {
print "200\x0D\x0A";
print "Content-Type: text/plain\x0D\x0A";
print "\x0D\x0A";
print threads->tid . "\x0D\x0A";
$req->Finish;
}
}
return;
}
Problem with blocking $req->Accept seems to be solved. As you can see, I
use IO::Select to make non-blocking check if socket ready to accept
connection.
Non-blocking accept is needed to give process possibility to process
received signals, because signals in threads is not OS level, but perl
emulated and don't break system calls.
$sel->can_read(1) return true in all threads, when new connection is ready
to be accepted.
I don't know, can system call $req->Accept() return positive value for
several callers simultaneously?
If it can - this code need to be modified.
Now, in my tests, it return 0+int to first caller, who really accept
connection, and negative value for all others.
On Fri, Aug 16, 2013 at 11:08 AM, Bugs in FCGI via RT
<bug-FCGI@rt.cpan.org>wrote:
Show quoted text>
> Greetings,
>
> This message has been automatically generated in response to the
> creation of a trouble ticket regarding:
> "make $request->Accept() non blocking",
> a summary of which appears below.
>
> There is no need to reply to this message right now. Your ticket has been
> assigned an ID of [rt.cpan.org #87892]. Your ticket is accessible
> on the web at:
>
>
https://rt.cpan.org/Ticket/Display.html?id=87892
>
> Please include the string:
>
> [rt.cpan.org #87892]
>
> in the subject line of all future correspondence about this issue. To do
> so,
> you may reply to this message.
>
> Thank you,
> bug-FCGI@rt.cpan.org
>
> -------------------------------------------------------------------------
> Is it possible to make $request->Accept() call unblocking, so i can use
> FCGI with threads and give thread a chance to receive and process signals?
>