Skip to main content

gocept.amqprun helps you writing and running AMQP consumers. It currently only supports AMQP 0-8 and integrates with the Zope Tool Kit (ZTK) so you can use adapters, utilities and all the buzz.

Project description

gocept.amqprun helps you writing and running AMQP consumers. It currently only supports AMQP 0-8 and integrates with the Zope Tool Kit (ZTK) so you can use adapters, utilities and all the buzz.

Basic concepts and terms

  • A message handler is a function which is bound with a routing key to exactly one queue. It is called for each message on that queue, and may return a list of messages as a result.
  • The result messages of one handled message are sent in one transaction together with the ACK of the handled message.
  • When an exception is raised during message processing, the transaction is aborted. (The received message would be NACKed if RabbitMQ was supporting it.)
  • A message handler handles exactly one message at a time. Multiple messages can be processed at the same time using threads. Those threads are called workers.

Things you don’t need to take care of

  • Threading of multiple workers
  • Socket handling and locking for communicating with the AMQP broker
  • Transaction handling

Getting started

To get started define a function which does the work. In this case, we log the message body and send a message. The handle decorator takes two arguments, the queue name and the routing key:

import logging
import gocept.amqprun.handler
import gocept.amqprun.message

log = logging.getLogger(__name__)

@gocept.amqprun.handler.handle('test.queue', 'test.routing')
def log_message_body(message):
    msg = gocept.amqprun.message.Message(
        body=u'Thank you for your message.',
    return [msg]

The handler function needs to be registered as a named utility. With ZCML this looks like this[1]_:

<configure xmlns="">
  <include package="gocept.amqprun" />
  <utility component="" name="basic" />

To set up a server, it’s recommended to create a buildout. The following buildout creates a config for gocept.amqprun, a ZCML file for the component configuration, and uses ZDaemon to daemonize the process:

parts =

name = queue
recipe = gocept.recipe.deploymentsandbox
root = ${buildout:directory}

recipe = lovely.recipe:mkfile
path = ${deployment:etc-directory}/queue.conf

amqp-hostname = localhost
amqp-username = guest
amqp-password = guest
amqp-virtualhost = /

eventlog =
      level DEBUG
        formatter zope.exceptions.log.Formatter
        path STDOUT
amqp-server =
      hostname ${:amqp-hostname}
      username ${:amqp-username}
      password ${:amqp-password}
      virtual_host ${:amqp-virtualhost}

content =
      amount 10
      component-configuration ${zcml:path}
      your.custom.settings here

recipe = lovely.recipe:mkfile
path = ${deployment:etc-directory}/queue.zcml
content =
    <configure xmlns="">
      <include package="gocept.amqprun" />
      <include package="your.package" />

recipe = zc.recipe.egg:script
eggs =
arguments = '${config:path}'
scripts = server=app

recipe = zc.zdaemonrecipe
deployment = deployment
program = ${buildout:bin-directory}/app


For application specific settings gocept.amqprun makes the <settings> section from the configuration available via an ISettings utility:

settings = zope.component.getUtility(


  • Currently all messages are send and received through the amq.topic exchange. Other exchanges are not supported at the moment.


0.2 (2010-09-14)

  • Added a decorator gocept.amqprun.handler.handle(queue_name, routing_key).

0.1 (2010-08-13)

  • first release.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
gocept.amqprun-0.2.tar.gz (21.1 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page