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

Hi monks,

I have not worked on threads. Could any one please help me out in writting a script to create a thread that wakes up every second, checks for existance of a file & reads the file if it exists. Could u please give me an outline of the script. I have absolutely no idea of how it looks like. Any suggestion will be of great help. You can also suggest me good websites to go through.

Thanks in advance

Nalina

Replies are listed 'Best First'.
Re: Thread to read a file every second
by BrowserUk (Patriarch) on Oct 06, 2004 at 10:25 UTC

    This will do what you ask.

    #! perl -slw use strict; use threads; use threads::shared; $|=1; my $data : shared; my $flag : shared = 0; sub watchit { my $file = shift; while( sleep 1 ) { if( -e $file ) { lock $data; open IN, '<', $file or die $!; $data = do{ local $/; <IN> }; close IN; unlink $file; lock $flag; $flag = 1; } } } threads->create( \&watchit, $ARGV[ 0 ] )->detach; my $n = 0; while( 1 ) { if( $flag ) { lock $data; print "\nNew file\n'$data'\n\n"; lock $flag; $flag = 0; $n = 0; } else { printf "\rNothing happened : %d", $n++; } select( undef, undef, undef, .1 ); } __END__ P:\test>396897 junk Nothing happened : 355 New file 'the file 'junk' contains this junk ' Nothing happened : 4103 New file 'the file 'junk' contains this junk ' Nothing happened : 62Terminating on signal SIGINT(2)

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
Re: Thread to read a file every second
by tachyon (Chancellor) on Oct 06, 2004 at 07:40 UTC

    It is not clear why you need threads to do this, more detail might be in order. perlthrtut would be a good point to start for info on threads.

    my $file = '/path/to/some/file'; while(1) { if ( -e $file ) { # read file # do stuff # unlink $file; # otherwise we will keep finding it } sleep 1; }

    If you want to use simple code like this you will want to daemonize it. There is a example of daemonizing on my scratchpad

    cheers

    tachyon

      tachyon: Isn't that an endless loop? Also, he probably wants threading so his program doesn't stick endlessly or wait for x number of seconds for each file that isn't there. Better to wait x seconds rather than xn seconds.

      EDIT TO PREVIOUS: I should have put in an end check parameter:

      sub check_file { my ($fpath, $arrp) = @_; my $max = 15; my $handle; while (!-e $fpath || !open($handle, $fpath)) { return if (!$max--); sleep(1); } $$arrp = join('', <$handle>); close($handle); }
Re: Thread to read a file every second
by TedPride (Priest) on Oct 06, 2004 at 08:13 UTC
    Neither have I, and my version of Perl doesn't support threading, but here's some code that might work:
    use threads; use strict; my @fpaths = ('fpath1', 'fpath2'); my %fdata; foreach (@fpaths) { my $data; my $child = threads->new(\&check_file, $_, $fdata{$_} = \$data); } sub check_file { my ($fpath, $arrp) = @_; my $handle; while (!-e $fpath || !open($handle, $fpath)) { sleep(1); } $$arrp = join('', <$handle>); close($handle); }
    I got my info on threading from:

    http://migo.sixbit.org/papers/Perl_Threads/slide-index.html

      Totally unrelated, how did my first post manage to show up underneath my second post?
        It didn't. This is called a threaded view (parent/child). By default its chronological. Example
        perlquestion - 1st reply to perlquestion - 1st reply to 1st reply to perlquestion - 2nd reply to perlquestion - 1st reply to 2nd reply to perlquestion - reply 3 - reply 4
        It doesn't matter if 1st reply to 1st reply to perlquestion got created after 2nd reply to perlquestion, its not a reply to perlquestion.

        MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
        I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
        ** The third rule of perl club is a statement of fact: pod is sexy.