Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

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

by clueless newbie (Friar)
on Sep 10, 2011 at 20:04 UTC ( #925280=note: print w/ replies, xml ) Need Help??


in reply to Re: Interrupting a blocking read (Linux::Inotify2) with a signal handler within a thread
in thread Interrupting a blocking read (Linux::Inotify2) with a signal handler within a thread

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.


Comment on Re^2: Interrupting a blocking read (Linux::Inotify2) with a signal handler within a thread
Select or Download Code
Re^3: Interrupting a blocking read (Linux::Inotify2) with a signal handler within a thread
by BrowserUk (Pope) on Sep 10, 2011 at 20:24 UTC

    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.

      That seems to be the best way to "get-her-done", but it galls me that I don't understand how to get a signal handler in a thread to work!

      I thank you for your help.

        it galls me that I don't understand how to get a signal handler in a thread to work

        The short answer is that they don't work in threads. Signals are sent to the process, not individual threads within it. The salient part of the relevant documentation for linux reads:

        A signal's disposition within a process's context defines what action the system will take on behalf of the process when a signal is delivered. All threads and LWPs (lightweight processes) within a process share the signal disposition, which is processwide and cannot be unique among threads within the same process.

        That said, threads belatedly acquired some additions that attempt to allow "inter-thread signalling", but in my limited experience of trying to use them, they are essentially useless, as they won't actually interrupt anything -- you have to poll to see if they have been received.


        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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2014-10-23 04:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (124 votes), past polls