Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Interrupting a blocking read (Linux::Inotify2) with a signal handler within a thread

by clueless newbie (Friar)
on Sep 10, 2011 at 19:19 UTC ( #925269=perlquestion: print w/ replies, xml ) Need Help??
clueless newbie has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to set up a ctrl/C handler in conjunction with a blocking read (Linux::Inotify2) within a thread. Neither of the approaches works, i.e. in neither case does the "Sighandler x tripped" ... "Exited while." appear. What do I need to do to get it to "behave"?

Thanks!
#!/usr/bin/perl use Cwd; use Data::Dumper; use Linux::Inotify2; use threads; use Thread::Queue; use Time::HiRes; use strict; use warnings; $|++; my($ToBeDone_q); my(@Worker_a); { $ToBeDone_q=Thread::Queue->new(); push(@Worker_a,threads->new(\&Watchdog,$ToBeDone_q)); }; # Process the queued up signals while (my($Request_s)=$ToBeDone_q->dequeue()) { warn ''.Data::Dumper->Dump([\$Request_s],[qw(*Request_s)]).' ' +; if ($Request_s eq 'All done!') { # Took one "All Done!" off last; } else { # Perform the work if possible my(%_h); eval $Request_s; # Do what you want to do with the results from the watchdo +g ToBeDone(); }; }; grep {$_->join;} @Worker_a; # Just In Case! while ( my(@list)=threads->list()) { print "$#list\n"; grep { $_->join } @list; }; exit; sub Watchdog { my($ToBeDone_q)=@_; print "Starting thread.\n"; my $Inotify_o=Linux::Inotify2->new(); $Inotify_o->watch(Cwd::abs_path('.'),IN_ALL_EVENTS); my $Done_f:shared; # Ctrl/c signal handler 1 - doesn't seem to work #$SIG{INT}=$SIG{TERM}=$SIG{HUP}=sub { # ? # # print "SignalHandler 1 tripped!\n"; # lock $Done_f; # $Done_f=1; # print "Attempting to unblock Inotify\n"; # $Inotify_o->blocking(0); # print "Inotify unblocked!\n" # }; # SignalHandler: Done # Ctrl/c signal handler 2 - doesn't seem to work ... sub _Sighandler { print "SignalHandler 2 tripped!\n"; lock $Done_f; $Done_f=1; print "Attempting to unblock Inotify\n"; $Inotify_o->blocking(0); print "Inotify unblocked!\n" }; # SignalHandler: Done #$SIG{INT}=$SIG{TERM}=$SIG{HUP}=\&_Sighandler; print "Starting while.\n"; while (!$Done_f) { print "Waiting on read.\n"; my @events_ao = $Inotify_o->read; print "read has been unblocked.\n"; unless (@events_ao > 0){ print "read error: $!"; } else { # To Do! foreach my $event_o (@events_ao) { print $event_o->fullname . " was modified\n" if $event +_o->IN_MODIFY; if ($event_o->fullname =~ m{\.pm$} && $event_o->IN_CLO +SE_WRITE) { # Have a module } elsif ($event_o->fullname =~ m{\.(?:cgi|pl)$} && $even +t_o->IN_CLOSE_WRITE) { # Have a script }; }; }; print "Continuing while.\n"; }; print "Exited while.\n"; print "Exiting thread.\n"; return; }; __END__

Comment on Interrupting a blocking read (Linux::Inotify2) with a signal handler within a thread
Download Code
Re: Interrupting a blocking read (Linux::Inotify2) with a signal handler within a thread
by BrowserUk (Pope) on Sep 10, 2011 at 19:45 UTC

    Can you interrupt a blocking Inotify read in a non-threaded script?


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      The following code does that.
      #! use Cwd; use Data::Dumper; use Linux::Inotify2; use strict; use warnings; my $Inotify_o=Linux::Inotify2->new(); $Inotify_o->watch(Cwd::abs_path('.'),IN_ALL_EVENTS); $|++; my $Done_f; # Ctrl/c signal handler --- works! $SIG{INT}=$SIG{TERM}=$SIG{HUP}= sub { # ? print "SignalHandler tripped!\n"; $Done_f=1; print "Attempting to unblock Inotify\n"; $Inotify_o->blocking(0); print "Inotify unblocked!\n" }; # SignalHandler: Done print "Starting while.\n"; while (!$Done_f) { print "Waiting on read.\n"; my @events_ao = $Inotify_o->read; print "read has been unblocked.\n"; unless (@events_ao > 0){ print "read error: $!\n"; } else { # To Do! foreach my $event_o (@events_ao) { print $event_o->fullname . " was modified\n" if $event_o->IN_M +ODIFY; if ($event_o->fullname =~ m{\.pm$} && $event_o->IN_CLOSE_W +RITE) { # Have a module } elsif ($event_o->fullname =~ m{\.(?:cgi|pl)$} && $event_o- +>IN_CLOSE_WRITE) { # Have a script }; }; }; print "Continuing while.\n"; }; print "Exiting!\n"; __END__

      Which yields

      perl sighandler.pl Starting while. Waiting on read. ^CSignalHandler tripped! Attempting to unblock Inotify Inotify unblocked! read has been unblocked. read error: Interrupted system call Continuing while. Exiting!

      That leads me to believe that if all else fails I can "invert" it and have the main do the watching/work submittal and have the worker perform the submitted work.

        Then your simplest, best (and probably only) solution would be to switch the notify and processing threads around.

        That is, put the event processing into the worker thread and run the Inotify in the main thread.

        That makes more sense anyway as the Inotify only has to queue the events, but if the event are occurring rapidly, a single processing thread might not be able to keep up. By placing the processing in a worker thread, you can start 2 or 3 or as many as are needed.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Interrupting a blocking read (Linux::Inotify2) with a signal handler within a thread
by salva (Monsignor) on Sep 10, 2011 at 20:27 UTC

    In my experience, the best (or easiest) way to handle that under Linux is to make the underlaying socket non-blocking and then use select to wait for input.

    Before the select, place a signal handler that would set a flag variable, and after the select, check if that variable has been set. To work around race-conditions, set a timeout in the select call:

    # untested: my $stop = 0; $SIG{INT} = sub { $stop = 1 }; while (1) { my $fileno = fileno($linux_notify2->socket); my $v = ''; vec ($v, $fileno, 1) = 1; select($v, undef, undef, 0.1); last if $stop; if (vec($v, $fileno, 1)) { $linux_notify2->poll; ... } }

    Another option, is to use one of the event frameworks available as POE or AnyEvent (though, mixing them with threads can be problematic at least).

      Thank you --- I'm hoping not to have to do that much rework as only the signal handler isn't working.

      I removed the Linux::Inotify2 code from the Watchdog and but still couldn't get either form of the signal handlers to work!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://925269]
Approved by BrowserUk
Front-paged by salva
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2014-09-20 05:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (153 votes), past polls