Should exist, but probably doesn't (2)

ti@lemonia.org ti at lemonia.org
Thu Dec 7 19:53:00 GMT 2006



There are certain operations, such as

* retrieving data from a remote database
* transcoding media
* sending HTTP requests, eg. when spidering websites

... which tend to be slow, and/or expensive. In either case, a program can
easily end up spending most of its time waiting for that operation to
happen.

There are lots of ways of handling this sort of thing, including:
+ Use multithreading, so other things happen while waiting for the
slow/expensive operations
+ Do something similar but with multiple processes
+ Do something similar all within a single thread/process that
approximates to the effect of threads (eg. POE)

Now, what I think I want is something along the lines of this model:

(in the module that does something-expensive)

sub convert_to_mp3 {
    my ($self, $filename) = @_;
    procrastinate($self, "_actually_convert_to_mp3");
}

sub _actually_convert_to_mp3 {
    my ($class, $filename) = @_;
    # actually transcode $filename
}

(in the script that's doing the transcoding)

Instead of:
foreach my $file (@files) { ... ->convert_to_mp3(...) };

this:
do_all_these ( @files, sub { $_->[0] ... convert_to_mp3(...) } });

do_all_these attempts to process each of the items given. When processing
an item means doing something expensive, the "procrastinate" call does
something appropriate to do the expensive task in the background (such as
handing processing off to a farm of servers, bunching several isomorphic
database queries together, or similar). It then *throws an exception*.
do_all_these catches it, and continues with the next item where possible.

The hope is that all this yields:
+ better scaleability than a single-thread single-process solution
+ code that runs the same way in sequential- and parallel processing mode
(eg. errors should, where possible, be propagated back to the caller).

Now, is there anything obviously insane about this, and/or, does it exist
already? I don't believe I've ever heard of it...

ti'






More information about the london.pm mailing list