Giter Club home page Giter Club logo

amqpipe's Introduction

AMQPipe

image

Requirements Status

image

image

image

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.

amqpipe's People

Contributors

fatal1ty avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Forkers

jazz-p davinirjr

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.