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

In my current application, I have a need to use threading to handle multiple instances of data that needs to be parsed in relative real time. Right now, I have everything working correctly to a point. Now with in the application I also need to run the application as a daemon.

The issue that I am seeing is that when the application iterates through the first time everything works fine. But as the second iteration starts the daemon dies. I have a small idea why it is happening, but not sure if it is correct.

So I come to you wise monks, humble as can be. Code below

Daemon.pm
package Lib::Daemon; use base qw(Exporter); our @EXPORT = qw(); use POSIX qw(setsid); use strict; sub daemonize { defined(my $pid = fork) or die "Can't fork: $!"; exit if $pid; print $pid . "PID WAS \n\n"; setsid() or die "Can't start a new session: $!"; } 1; __END__


Main.pl
Lib::Daemon::daemonize(); print "Starting Daemon.... \n\n"; my @thr = (); while(1){ main(); sleep(60); } sub threadMain{ print "Child Thread $_[0] \n"; print "*********************************************************** +*********************\n"; parseMain($_[0],$_[1]); return 1; } sub main{ my @instData = Lib::PLSInstance::getInstances(); for(my $i = 0; $i < @instData; $i++){ $thr[$i] = threads->new(\&threadMain, $instData[$i][2],$instDa +ta[$i][1] ) or die "Thread Died: $!"; } # start rejoining the threads for (my $i = 0; $i < @instData; $i++) { print "Joining thread $i\n"; $thr[$i]->join() or die "Thread die on join: $!"; print "\tThread $i joined back with the parent\n"; } }

Replies are listed 'Best First'.
Re: Threading + Daemons troubles
by zentara (Cardinal) on Aug 21, 2008 at 19:55 UTC
    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.
Re: Threading + Daemons troubles
by zentara (Cardinal) on Aug 21, 2008 at 17:10 UTC
    But as the second iteration starts the daemon dies

    That is the number one symptom( not 100% positive), that the first run's threads are still partially hanging around, and causing confusion, and your failure. Are you sure they joined? A thread must return from it's code block to be joinable. So you need something in your thread code block, to tell that thread to return. Just telling it to join, will NOT do it. The latest version of threads will not join if the thread has any piped opens left open, so you must kill any pids the thread launched. The latest threads version does has a way to summarily kill a thread, without it returning. What version of Perl you running?

    In Perl, the thread gets an exact copy of the parent, at the time of spawning.... that includes any previous spawned threads. This makes it very tricky to dynamically spawn threads, as you do. You can try killing the thread (or forcing it to return), but that is no gaurantee that any objects you used got cleaned up, causing part of the thread to linger around.

    The only reliable solution, is to reuse your threads. Don't try to dynamically spawn and join.....even if you get it to work, you will get a memory gain each spawn cycle. So reuse your threads, and preferrably create all of them before you demonize the main thread. See Reusable threads demo.


    I'm not really a human, but I play one on earth Remember How Lucky You Are
Re: Threading + Daemons troubles
by BrowserUk (Patriarch) on Aug 21, 2008 at 17:12 UTC
    But as the second iteration starts the daemon dies. I have a small idea why it is happening, but not sure if it is correct.

    What output do you see just prior to it dying? Why not share you small idea?


    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: Threading + Daemons troubles
by BrowserUk (Patriarch) on Aug 21, 2008 at 17:27 UTC