Last active
August 29, 2015 14:10
-
-
Save benwills/a293b758de19d6c736cb to your computer and use it in GitHub Desktop.
Template: Perl Thread Queue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! perl -slw | |
use threads stack_size => 8*4096; | |
use Thread::Queue; | |
############################################################ | |
# | |
# There are pipe errors that happen. I came across only | |
# one, but it breaks the code entirely. | |
# | |
# Gives broken pipe error: | |
# http://www.invesp.com/blog/cro/ecommerce-and-the-hammer.html | |
# | |
# Solution from: | |
# http://www.perlmonks.org/?node_id=828184 | |
# | |
############################################################ | |
$|++; | |
$SIG{PIPE} = 'IGNORE'; | |
our $THREADS = 250; | |
my $Q = new Thread::Queue; | |
my @threads = map async( sub { | |
while( my tmp = $Q->dequeue() ) { | |
############################################################ | |
# | |
# Do the work | |
# | |
############################################################ | |
############################################################ | |
# | |
# A note on killing threads... | |
# | |
# A local sig alarm was not working well. It killed the | |
# program. Instead, what seemed to work is, instead of | |
# killing a thread, sending it to a position later in | |
# the code: | |
# | |
# if (x) else { goto THREADEND; } | |
# THREADEND: | |
# | |
############################################################ | |
} | |
} ), 1 .. $THREADS; | |
while( <> ) { | |
chomp; | |
sleep 1 if $Q->pending > $THREADS; | |
$Q->enqueue( $_ ); | |
} | |
$Q->enqueue( (undef) x $THREADS ); | |
$_->join for @threads; | |
__END__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment