Saltycrane logo

SaltyCrane Blog

Notes on Python, Django, and web development on Ubuntu Linux

    

Notes on Python and beanstalkd on Ubuntu Karmic

Beanstalk

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

Post a comment

Required
Required, but not displayed
Optional

Format using Markdown. (No HTML.)
  • Code blocks: prefix each line by at least 4 spaces or 1 tab (and a blank line before and after)
  • Code span: surround with backticks
  • Blockquotes: prefix lines to be quoted with >
  • Links: <URL>
  • Links w/ description: [description](URL)
Created with Django | Hosted by Linode