Skip to main content

Twisted based pipeline framework for AMQP

Project description

https://img.shields.io/pypi/dm/amqpipe.svg?maxAge=2592000 https://img.shields.io/pypi/v/amqpipe.svg?maxAge=2592000 https://img.shields.io/pypi/pyversions/amqpipe.svg?maxAge=2592000 https://img.shields.io/badge/license-MIT-blue.svg?maxAge=2592000

Twisted based pipeline framework for AMQP. It allow you to create fast asynchronous services which follow ideology:

  • get message from queue

  • doing something with message

  • publish some result

Installation

Install via pip:

pip install amqpipe

Basic usage

The minimal module based on AMQPipe is:

from amqpipe import AMQPipe

pipe = AMQPipe()
pipe.run()

It will simply get all messages from one RabbitMQ queue and publish them to other RabbitMQ exchange.

Now we define some action on messages:

import hashlib
from amqpipe import AMQPipe

def action(message):
    return hashlib.md5(message).hexdigest()

pipe = AMQPipe(action=action)
pipe.run()

It will publish md5 checksum for every message as result.

If messages in input queue are in predefined format then you can define converter-function:

import hashlib
from amqpipe import AMQPipe

def converter(message):
    return message['text']

def action(text):
    return hashlib.md5(text).hexdigest()

pipe = AMQPipe(converter=converter, action=action)
pipe.run()

You can define service-specific arguments:

import hashlib
from amqpipe import AMQPipe

class Processor:
    def set_field(self, field):
        self.field = field

processor = Processor()

def init(args):
    processor.set_field(args.field)

def converter(message):
    return message.get(processor.field)

def action(text):
    return hashlib.md5(text).hexdigest()

pipe = AMQPipe(converter, action, init)
pipe.parser.add_argument('--field', default='text', help='Field name for retrieving message value')
pipe.run()

You can connect to database in init function or do some other things for initialization.

If your action returns Deferred then result would be published to RabbitMQ when this Deferred will be resolved:

import logging
from twisted.internet import defer
from amqpipe import AMQPipe

logger = logging.getLogger(__name__)

class Processor:
    def set_field(self, field):
        self.field = field

processor = Processor()

def init(args):
    connect_to_db()
    ...

def converter(message):
    return message.get(processor.field)

@defer.inlineCallbacks
def action(text):
    result = yield db_query(text)
    logger.info('Get from db: %s', result)
    defer.returnValue(result)

pipe = AMQPipe(converter, action, init)
pipe.parser.add_argument('--field', default='text', help='Field name for retrieving message value')
pipe.run()

Init function may return Deferred too.

Project details


Download files

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

Source Distribution

amqpipe-0.2.7.tar.gz (5.8 kB view details)

Uploaded Source

File details

Details for the file amqpipe-0.2.7.tar.gz.

File metadata

  • Download URL: amqpipe-0.2.7.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for amqpipe-0.2.7.tar.gz
Algorithm Hash digest
SHA256 49f22992690e776d061910f4b4066d625d397f33047ce6742f25a4ff2a2667b9
MD5 f25b19a8a25bf0b1061ac67b6f476775
BLAKE2b-256 ee0a208dbc4c91004fe3bbc59ff1fd0115dda35fbe6843f25e8d18d0c8feed47

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page