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

Hi,
I was thinking of a way to improve the speed of an e-mailing script where a bottleneck has arisen because of serial SMTP sending.
I'd like to "parallelize" the processing of the address array into 2-4 parallel processes, each sending via SMTP their parts of the array.
How would I split processing of the @email_addresses like this? create 2-4 sub-arrays? fork the sending once the sub-array's are populated?
Basically I set the e-mail message var's once (body, subject, from) and then do a foreach loop on an array of e-mail addresses, sending the message sequentially.
Looking fwd to some Monk-wisdome

It sounds like you are on the right track. I'd suggest the pre-forked
server design as detailed in the Perl Cookbook. Basically,
you fork off a process for each mail address until you have hit
your desired max number of processes. Then wait for each child
to finish and start another process at that time. Repeat until done.
That will get the task done as quickly as possible, at some expense
in space.
From another program I wrote:

Have you looked into POE. Although I have not used it (for anything except little tests) and have only seen a demonstration on it, it looks like a good fit for your problem. You can easily feed you array to several different handlers (that will fork off for you).

I just got done writing an application that does exactly what you want to do. I will give you this one bit of advice. Don't use a foreach loop, like:
for my $email (@emails){
&nbsp&nbsp&nbsp&nbsp#fork();
}

It will reduce memory usage. At least over time. I'm sure there is a better way, but it works for me.

To handle the message array processing. I set up @mime_data, @text_data array's. Then in my send code something like this:
for (@mime_data){
&nbsp&nbsp&nbsp&nbsps/PATTERN/$var/g;
&nbsp&nbsp&nbsp&nbsppush(@foo,$_);
}
Net::SMTP->data("To: $email",@foo);
So I obviously use Net::SMTP. That is the only module I use, I suggest you do the same unless you want to bloat your code - or you know more than I do. :)