According to its website, Beanstalkd is a fast, distributed, in-memory workqueue service. In simpler terms this means clients can add tasks to a queue and workers will execute these tasks whenever they have time.
I will show how this works using the perl client libraries. The Beanstalk::Client libraries can be found on the CPAN.
Both the clients and the workers use the same library. The following example will put a ‘print’ task to the queue.
use Beanstalk::Client;
my $client = Beanstalk::Client->new({
server => "localhost",
default_tube => 'mine',
});
$client->put({}, 'print', 'Hello world');
A worker for this task could look like this.
use Beanstalk::Client;
my $client = Beanstalk::Client->new({
server => "localhost",
default_tube => 'mine',
});
for (;;) {
my $job = $client->reserve();
next unless $job;
my @args = $job->args();
if ($args[0] eq 'print') {
print $args[1] . "\n";
$job->delete();
}
}
After you create these two programs, you will have to start beanstalkd
. You
can use the following command.
./beanstalkd -l 127.0.0.1 -p 11300
Then start the worker and the client program. You will see how the worker will execute the task that were added by the client program. If you start multiple workers, they will divide the tasks between the two programs. This way you can add more workers if you need more processing power.