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

Hi to all, I have been looking through the forums for an answer to my question and believe that Random has an answer to my question, but to be honest I'm only a beginner and cannot adapt his code to my uses, so if somebody could spare a little code my way I'd be amazingly grateful. So here goes my problem. I runa a mailing list (approx 25,000 subscribers) with a perl script. No problems until i try to mail everybody and at this point the script times out after about 10,000 mails forcing me to send the mail in three batches. This is a little bit of a pain. Is there a way of automating the process. Here is the code i use:
...beginning of subroutine $pid = fork(); $pid; if ($pid) { print "The mailing has began"; exit(0); } else { close (STDOUT); ...actual mailing
Now, I am convinced that it is possible to avoid the timeout problem using multiple forks. Is that a possibility? Or is there something better? Thanks a lot for your help, Adrien

Replies are listed 'Best First'.
Re: fork and mailing lists
by joealba (Hermit) on Nov 03, 2001 at 19:59 UTC
    spaz and TheoPetersen were right. ForkManager is good stuff! Here's the sample code:
    use Parallel::ForkManager; # Max number or processes you want to fork at one time. # Set this to something reasonable so you don't thrash your box. my $MAX_PROCESSES = 5; # J. Random list of numbers. my @things = (1..10); my $pm = new Parallel::ForkManager($MAX_PROCESSES); # Start looping through my list of numbers foreach my $thing (@things) { # This is more of a loud comment than anything else, # just telling you that the parent is about to # fork a child. print "Forking off $thing...\n"; # Kick off the child process. # If this is the parent process, # we'll move on to the next step in this loop $pm->start and next; # This is your child process. # There are many like it, but this one is yours. :) # Do your forked goodness here print "$thing\n"; $pm->finish; # The child has finished. Tell him to clean up his room. } $pm->wait_all_children;
    Just keep your $MAX_PROCESSES at something reasonable for your system, and all should be fine.

    Updated: Added comments. In this code, I have a list of numbers. I loop through that list of numbers and fork off child procs which simply print that number.

    I'm sorry to say this, but if you can't extend this code to do what you want, you really shouldn't be writing this code. You must always practice safe forking.
      Thanks joealba,
      But I'm not good coder, could you explain please.
      Where does your code go in mine?
      Could you comment your variables?

      Thanks again,
      Adrien