Simple queue in Redis with Perl

Sometimes you need to have a asynchronous worker written in Perl. The small script here takes a job from the queue and executes the code. It only takes a few lines of Perl.

use Redis;
my $redis = Redis->new(encoding => undef);

my $queue_name = 'q';
my $timeout    = 10;

for (;;) {
    my ($queue, $job_id) = $redis->blpop(join(':', $queue_name, 'queue'), $timeout);
    if ($job_id) {

        my %data = $redis->hgetall($job_id);

        # do something with data...
        # ...

        # remove data for job
        $redis->del($job_id);
    }
}

The client would look like this:

use Redis;
my $redis = Redis->new(encoding => undef);

my $queue_name = 'q';

# Create the next id
my $id = $redis->incr(join(':',$queue_name, 'id'));
my $job_id = join(':', $queue_name, $id);

my %data = ();

# Set the data first 
$redis->hmset($job_id, %data);

# Then add the job to the queue
$redis->rpush(join(':', $queue_name, 'queue'), $job_id);

This type of queue is simple to create. This version just takes a few lines of code. It only depends on the Redis module and a running Redis server. The Redis module connects to $ENV{REDIS_SERVER}, so this could be changed before running the worker script.

Welcome

My name is Peter Stuifzand. You're reading my personal website.

Profiles