NAME

Consumer::NonBlock - Send data between processes without blocking.

DESCRIPTION

It is very easy to end up in a situation where a producer process produces data faster than a consumer process can read/process it resulting in the producer blocking on a full pipe buffer. This module allows 2 processes to share data similar to a pipe, but without the producer blocking due to full pipe buffers.

A pipe is better in most situations, this is only useful if the producer needs to do many things and you cannot afford to block on a consumer. This is used by App::Yath to send data to a comparatively slow database upload process without blocking.

SYNOPSIS

use Consumer::NonBlock;

my ($reader, $writer) = Consumer::NonBlock->pair(batch_size => 100);

$writer->write_line("A line!");
$writer->write("several\nlines\n", "in\nseveral\nstrings\n");

my $line1 = $reader->read_line();
# "A line!"

my @lines = $reader->read_lines();
# "several"
# "lines"
# "in"
# "several"
# "strings"

$writer = undef; # Close the output
$reader = undef; # Close the input, and delete the temp dir

SYNOPSYS WITH FORK

Normally when the reader is closed it will delete the data. Normally when the writer is closed it will mark the data stream as complete. If you fork then either of these actions can happen in either process.

The weaken() method can be used to prevent a reader or writer from taking these actions. So in the producer process you want to weaken the reader object, and in the consumer process you want to weaken the writer object.

use Consumer::NonBlock;

my ($reader, $writer) = Consumer::NonBlock->pair(batch_size => 100);

my $pid = fork // die "Could not fork: $!";

if ($pid) { # Parent
    # Make sure this process does not delete the temp data
    $reader->weaken;
    $reader->close;

    $writer->write_line("Line from the parent");
}
else { # Child
    # Make sure this process does not mark the IO as complete
    $writer->weaken;
    $writer->close;

    my $line = $reader->read_line;
    print "Got line: $line\n";
}

IMPLEMENTATION DETAILS

This module works by having the producer write to temporary files. It will rotate files after a specified batch limit. The consumer will delete the files as it finishes with them to prevent having data we already processed sitting on disk. For best performance /var/shm should be used.

METHODS

SOURCE

The source code repository for Consumer-NonBlock can be found at http://github.com/exodist/Consumer-NonBlock/.

MAINTAINERS

AUTHORS

COPYRIGHT

Copyright Chad Granum exodist7@gmail.com.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

See http://dev.perl.org/licenses/