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 thr +eads 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.


In reply to Re^2: [FCGI.pm] Possibility to make $request->Accept() non blocking by zdm
in thread [FCGI.pm] Possibility to make $request->Accept() non blocking by zdm

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.