Acme-Spork

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

Revision history for Perl extension Acme::Spork.

0.0.1  Fri Jan  6 12:02:38 2006
	- original version; created by h2xs 1.23 with options
		-AXc -n Acme::Spork

0.0.2  Mon Jan  9 20:36:42 2006
	- made spork return PID of sporked process

0.0.3  Mon Jan  9 20:36:78 2006
	- realized I hadn't put this in the change log after uploading 0.0.2, doh!
        - or updated the version in README
	- added "the" to the POD near plastic for good measure
	- make spork() return (instead of croak) if fork fails so you can handle the failure as need be
	- added child sig so zombies do not happen, we hope anyhoo ;p
        - slowed down to hopefully not miss the stupid stuff

0.0.4  Tue Sep 12 20:38:30 CDT 2006

Changes  view on Meta::CPAN

	- local()ize'd $SIG{'CHLD'}

0.0.6  	Tue Jan 16 23:54:01 2007
    - added @reopen_to

0.0.7  Wed Jan 17 09:52:55 2007
	- changed 0.0.6 to %reopen_stdfhs_to for clarity
	
0.0.8  Wed Apr  1 21:52:29 2009
    - Double fork to avoid needing to munge $SIG{CHLD} outside of spork()
    - pipe() to get spork()ed process's PID back for return
    - $SIG{CHLD} so that $? inside spork() is correct (i.e. not -1 for auto reaped)
    - the 3 things above also make it zombie safe for when the parent outlives the child
    - explicit exit after coderef call to avoid "fork doubling"
    - Ability to define Acme::Spork::setsid() to not use POSIX::setsid() (i.e. to not load POSIX)

lib/Acme/Spork.pm  view on Meta::CPAN

    spork( 
        sub { 
            sleep 5;
            open my $log_fh, '>>', 'spork.log', or die "spork.log open failed: $!";
            print {$log_fh} "I am spork hear me spoon\n"; 
            close $log_fh;
        },
    ) or die qq{Couldn't fork for spork: $!};
    print 2;

This prints out "12" immediately and is done running, now if you tail -f spork.log you'll see "I am spork hear me spoon\n" get written to it 4 or 5 seconds later by the spork()ed process :)

spork() returns the PID of the spork()ed process so you can keep track of them and do what you need with it.

If it returns false then fork failed so you can:

    if(spork(\&foo)) {
        print "I am spork here me spoon\n";
    }
    else {
        print "Could not fork for spork: $!";
    }
    my $spork_pid = spork(\&foo) or die "Could not fork for for spork: $!";

lib/Acme/Spork.pm  view on Meta::CPAN


or if you already have the module loaded:

  *Acme::Spork::setsid = *POSIX::Subset::setsid;
   my $pid = spork(...);

It will use your setsid() instead and POSIX will not be brought in. If it doesn't actually setsid() then you just broke yourself so don't do that.

=head1 daemonize()

Since many daemons need to spork a child process when a request is received I've included a cheat function to daemonize your script execution.

Its simply a wrapper for Proc::Daemon::Init.

    use Acme::Spork qw(daemonize);

    # make sure we are the only one running:
    use Unix::Pid '/var/run/this.pid';

    # if so make me a daemon:
    daemonize();



( run in 0.301 second using v1.01-cache-2.11-cpan-8d75d55dd25 )