Git Product home page Git Product logo

flask-pika's Introduction

This is a fork of flask-pika with proposed changes from this PR.


Pika Extension for Flask

This extension provides a simple way to expose a Pika blocking channel inside of Flask.

Once a channel is obtained, use it as you would any normal Pika blocking channel.

Initializing the Pika object

Add the Flask Pika Params to your app config and then initialize the Flask Pika instance with a your app instance.

##config.py
FLASK_PIKA_PARAMS = {
    'host':'amqp host',      #amqp.server.com
    'username': 'username',  #convenience param for username
    'password': 'password',  #convenience param for password
    'port': 5672,            #amqp server port
    'virtual_host':'vhost'   #amqp vhost
}

# optional pooling params
FLASK_PIKA_POOL_PARAMS = {
    'pool_size': 8,
    'pool_recycle': 600
}


##app.py
from flask import Flask
from flask.ext.pika import Pika as FPika

app = Flask(__name__)
fpika = FPika(app)

# Alternatively, Flask's application factory pattern is supported:

fpika = Fpika()
# Then, later...
fpika.init_app(app)

Connection pooling

If the optional FLASK_PIKA_POOL_PARAMS are specified in your app config, then channels will be allocated via a pool of channels.

  • pool_size: number of channels to have open at any one time
  • pool_recycle: amount of time in seconds before a channel is closed and it's replaced in the pool (internally this is done on checkout)

Each pool will be allocated per process and the pool will be shared amongst the threads in the process.

Using the Pika object

Use the pika object you created and get a Pika blocking channel.

ch = fpika.channel();
ch.basic_publish(exchange='exchange',routing_key='routing_key',body='message')
fpika.return_channel(ch);

Resource handling

Any pika channel obtained via the fpkia.channel() call must be returned via either the fpika.return_channel(channel) call or the fpika.return_broken_channel(channel) call.

The return_channel call should be used under normal circumstances and the return_broken_channel call should be used if the channel is known to be broken.

If the return channel calls are not used, then Pika connections will be leaked.

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.