zdm has asked for the wisdom of the Perl Monks concerning the following question:

Hello. 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?
  • Comment on [FCGI.pm] Possibility to make $request->Accept() non blocking

Replies are listed 'Best First'.
Re: [FCGI.pm] Possibility to make $request->Accept() non blocking
by Corion (Patriarch) on Aug 16, 2013 at 12:01 UTC

    Unless you tell us more about your infrastructure, the answer is likely to be "no".

    As an approach, I could see to launch the FCGI server part in one thread and launch some more worker threads separately.

    You should be advised that mixing threads and signals is highly OS+clib specific and most likely does not do what you want. Maybe only your main thread will receive signals, but maybe all threads will receive signals, or just a random set.

      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.

        This solution is not work under MSWin, because:
        select (the underlying system call used by IO::Select) only works for +sockets in Windows.
        and FCGI.pm seems use pipe or something else in windows. Please, help resolve this problem.