Just for jollies, I tried to set up my Reusable threads demo to run as a daemon, and it runs fine. This is full of goto's(I would use a Glib event loop instead), but it works and tracks output to a file. It will rerun itself about every minute. I guess you will need to open a socket to the daemon, to send commands to it.
#!/usr/bin/perl use strict; use warnings; use threads; use threads::shared; use POSIX; # on linux, watch with "ps -T uxww" to see threads # daemonize if(1) { exit if fork; POSIX::setsid(); } # script goes here open (FH,">$0.out")or die "$!\n"; #prints go to FH select(*FH); $| = 1; # Turn on autoflush for FH my %shash; my @to_be_processed = ('a'..'z'); my @ready:shared = (); my $numworkers = 10; foreach my $dthread(1..$numworkers){ share $shash{$dthread}{'go'}; $shash{$dthread}{'go'} = 0; share $shash{$dthread}{'fileno'}; #in case you want $shash{$dthread}{'fileno'} = ''; #shared filehandles share $shash{$dthread}{'data'}; $shash{$dthread}{'data'} = ''; share $shash{$dthread}{'pid'}; $shash{$dthread}{'pid'} = -1; share $shash{$dthread}{'die'}; $shash{$dthread}{'die'} = 0; $shash{$dthread}{'thread'} = threads->new(\&worker, $dthread); push @ready, $dthread; } #print "\t\t",scalar @ready," threads ready.......press a key to start + threads\n"; #<>; RUN: print FH "Running again\n"; @to_be_processed = ('a'..'z'); #reload fake data while (my $t = shift(@ready)){ $shash{$t}{'data'} = shift @to_be_processed; $shash{$t}{'go'} = 1; } while(1){ if( scalar @ready > 0 ){ if( my $data = shift @to_be_processed ){ my $t = shift(@ready); $shash{$t}{'data'} = $data; $shash{$t}{'go'} = 1; #print "thread $t restarting\n"; }else{ print "out of input\n"; goto WAIT; } } } WAIT: #print "at end loop\n"; print FH "sleeping\n"; for(1..10){ sleep(1) } print "restarting another run\n"; goto RUN; ###############################################3 sub worker{ my $thr_num = shift; #print "$thr_num started\n"; my $count; START: while(1){ if( $shash{$thr_num}{'die'} ){ #print "thread $thr_num finishing\n"; return} #wait for $go_control if($shash{$thr_num}{'go'}){ if($shash{$thr_num}{'die'}){ #print "thread finishing\n"; return} $count++; my $str = ' 'x$thr_num; #printout spacer #print $str.$thr_num.'->'.$count.$shash{$thr_num}{'dat +a'},"\n"; if ($count > 10){ goto RECYCLE; } select(undef,undef,undef,.1); #sleep rand 5 }else{ $count = 0; select(undef,undef,undef,.1); }# sleep until awakened } #end while(1) RECYCLE: $shash{$thr_num}{'go'} = 0; #print "$thr_num done....going back to sleep\n"; $shash{$thr_num}{'data'} = ''; $count = 0; push @ready, $thr_num; #print "pushing $thr_num\n"; goto START; return; }
</reaedmore>

I'm not really a human, but I play one on earth Remember How Lucky You Are

In reply to Re: Threading + Daemons troubles by zentara
in thread Threading + Daemons troubles by rottmanja

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.