Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: [FCGI.pm] Possibility to make $request->Accept() non blocking

by Corion (Patriarch)
on Aug 16, 2013 at 12:01 UTC ( [id://1049731]=note: print w/replies, xml ) Need Help??


in reply to [FCGI.pm] Possibility to make $request->Accept() non blocking

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.

  • Comment on Re: [FCGI.pm] Possibility to make $request->Accept() non blocking

Replies are listed 'Best First'.
Re^2: [FCGI.pm] Possibility to make $request->Accept() non blocking
by zdm (Beadle) on Aug 18, 2013 at 11:23 UTC
    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1049731]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-19 02:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found