Notes on Python and beanstalkd on Ubuntu Karmic
Install and run beanstalkd
Reference: beanstalkd README
- Install dependencies (libevent 1.4.1+)
$ sudo apt-get update $ sudo apt-get install libevent-1.4-2 libevent-dev
- Download and unpack beanstalkd
$ curl http://xph.us/dist/beanstalkd/beanstalkd-1.4.4.tar.gz | tar zx
- Build
$ cd beanstalkd-1.4.4 $ ./configure $ make
- Run the beanstalkd server
$ ./beanstalkd -d -l 127.0.0.5 -p 11300
Install the Python client library
- Install pip
- Install PyYAML (beanstalkc dependency)
$ sudo apt-get build-dep python-yaml $ sudo pip install PyYAML
- Install beanstalkc
$ sudo pip install beanstalkc
Example
Reference: beanstalkc TUTORIAL
import beanstalkc
# connect to server
bean = beanstalkc.Connection(host='127.0.0.5', port=11300)
# put jobs in queue
for i in range(5):
print 'Put data: %d' % i
bean.put(str(i))
# get jobs from queue
while True:
job = bean.reserve()
print 'Got data: %s' % job.body
job.delete()
Results:
Put data: 0 Put data: 1 Put data: 2 Put data: 3 Put data: 4 Got data: 0 Got data: 1 Got data: 2 Got data: 3 Got data: 4