Bala's Blog

Beanstalk and Python in FreeBSD

December 22 2016
160 words, ~1 min. read
beanstalk,  freebsd,  python,  worker,  queue 

Beanstalk is a simple, fast work queue, inspired by the simplicity of memcached. Like memcached, it has rich client support in multiple languages. Being a text protocol, writing a new client is easy (if not already available).

Server

Install

pkg install beanstalkd

Run daemon

The daemon could be run using any of the following,

  1. beanstalkd
  2. beanstalkd -l 127.0.0.1
  3. beanstalkd -l 192.168.1.11 -p 11301
  4. beanstalkd -b <log dir>

The -l and -p arguments are used to specify listen address (default 0.0.0.0) and port (default 11300) accordingly. The -b argument accepts a directory location for binary logging and is useful to make the queues persistent across restarts.

Client

Install

$ sudo pkg install py27-virtualenv
$ virtualenv worker
$ . worker/bin/activate
(worker) $ pip install yolk beanstalkc pyyaml

producer.py

#!/usr/bin/env python

import beanstalkc

b = beanstalkc.Connection(host='192.168.1.11')
b.put('Hello')

consumer.py

#!/usr/bin/env python

import beanstalkc

b = beanstalkc.Connection(host='192.168.1.11')
job = b.reserve()
print( job.body )
job.delete()

TIPS

  1. Use beanwalker to monitor
  2. Refer beanstalkc tutorial for more information