in reply to create new task from main program

And here's some working threads code to get you started.

Note:depending upon what you are doing in your sub, and whether you need the results from the subs back in the main program, it could get more complicated than this--but it will at least be doable.

#! perl -slw use strict; use threads; $|=1; my @threads; for my $i ( 1 .. 10 ) { ## if the condition is true if( $i & 1 ) { ## every odd number matches ## Start a thread to run the sub ## Passing the number as an argument ## And saving the thread object, push @threads, async \&other, $i; } } ## Wait for all the threads to finish $_->join for @threads; ## Done. exit; sub other{ my( $number ) = @_; print "Other( $number ) starting"; ## pretend we're doing something that takes a while... for( 1 .. rand 10 ) { print "Other( $number ) busy ..."; sleep 1; } print "Other( $number ) finished"; } __END__ C:\test>542468 Other( 1 ) starting Other( 1 ) busy ... Other( 3 ) starting Other( 3 ) busy ... Other( 5 ) starting Other( 5 ) busy ... Other( 7 ) starting Other( 7 ) busy ... Other( 9 ) starting Other( 9 ) finished Other( 1 ) busy ... Other( 3 ) finished Other( 5 ) busy ... Other( 7 ) busy ... Other( 1 ) busy ... Other( 5 ) finished Other( 7 ) busy ... Other( 1 ) busy ... Other( 7 ) busy ... Other( 1 ) busy ... Other( 7 ) busy ... Other( 1 ) busy ... Other( 7 ) busy ... Other( 1 ) busy ... Other( 7 ) busy ... Other( 1 ) finished Other( 7 ) busy ... Other( 7 ) finished

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: create new task from main program
by benlaw (Scribe) on Apr 12, 2006 at 16:40 UTC
    Hi ,
    I try to combined the code with threads,

    but it shows "thread failed to start: Not a CODE reference at D:/Perl/lib/threads.pm" is that any go worse?
    use strict; use Win32::ChangeNotify; use Win32::IPC; use File::Basename; use File::Glob; use DBI; use threads; my $path = 'c:\temp\buffer\event'; #$|=1; my @threads; ### Main program part my $notify = Win32::ChangeNotify->new( $path, 0, 'FILE_NAME' ); my %last;@last{ glob $path . '\*' } = (); CHECK_MAIN(); while( 1 ) { $notify->wait; $notify->reset; my @files = glob $path . '\*'; if(@files == scalar keys %last) { my %temp; @temp{ @files } = (); delete @temp{ keys %last }; foreach my $file (keys %temp){ my $currentFile = basename $file; if ($currentFile =~ /\.log$/){ push @threads, async \&READ_FILE($file); # READ_FILE("$file"); } } } undef %last; @last{ @files } = (); } $_->join for @threads; exit 0; sub CHECK_MAIN{ } sub READ_FILE{ my $file = shift; my $pattern; my $errMsg; open(F, "unix.txt")|| die "unix\.txt not found\nopen : $!"; while(<F>){ chomp($_); $pattern .= $_."|"; } close(F); chop($pattern); my (@errlog, @normal) = (); chomp($file); $file =~ s/\\/\//g; sleep 1; open(TARGET, $file) || die "$file not found\nopen : $!"; while (<TARGET>){ if(/$pattern/ig){ next if(/root\-oracle/ig); chomp($_); my $temp = "$&\|".$_; push (@errlog, $temp); }else{ push (@normal, $_); } } close(TARGET) || warn "close : $!"; # print "@errlog"; #SEND_SMS(\@errlog) if(@errlog > 0); #ADD_DATA(\@errlog) if(@errlog > 0); } # END of READ_FILE

      Changing

      push @threads, async \&READ_FILE($file);

      to

      push @threads, async \&READ_FILE, $file;

      should cure that problem.

      Ie. You need to pass the address of the subroutine, and the parameter as separate items. What you were doing is calling the function and then passing the address of the return value to async

      HTH.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Thanks a lot ^^, moreover, thanks for your win32::changenotify 's code (http://perlmonks.org/?node_id=306191), it's a very good reference for the module!
        Ooop , 1 more problem ,
        I found the program memory grows larger and not free up, i try to use threads->detach() but it seems cannot detach from memory