Giter VIP home page Giter VIP logo

flask-pika's Introduction

#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.

flask-pika's People

Contributors

maximium avatar mniehe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

flask-pika's Issues

KeyError when checking if channel should be recycled

After 16 requests, the error starts occurring.

The params:

app.config['FLASK_PIKA_POOL_PARAMS'] = {
    'pool_size': 16,
    'pool_recycle': 600
}

The trace stack is shown below

File "D:\project\controllers\pikatest.py", line 32, in test_place
    ch = pika.channel()
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\flask_pika.py", line 132, in channel
    if self.__should_recycle_channel(ch):
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\flask_pika.py", line 115, in __should_recycle_channel
    recycle_time = self.channel_recycle_times[hash(channel)]
KeyError: 4410099

The code that gets executed every request.

ch = pika.channel()
ch.basic_publish(exchange='', routing_key='place', body=json.dumps(body))
pika.return_channel(ch)

upgrade pika

your pika requirement in the setup.py is quite old. using pipenv with a newer version of pika complains about it, because I manually have pika in it w/ the newest version. could you update the repo quickly to make it >0.10.0 and release again?

thx

Pool Params isn't optional

Leaving FLASK_PIKA_POOL_PARAMS out of the config causes an error. The variable needs to exist but set as None to not use the pooling features.

Unable to run the publisher and flask app simultaneously

Hello,

I am trying to use your library to be able to run multiple pika subscribers/publishers in a flask app. I think I am not using the library right because once I run my code, I am not able to see the flask app started message on my console. Below is the code I wrote using your guidelines :

from flask import Flask
from flask_pika import Pika as FPika

app = Flask(__name__)
app.config['FLASK_PIKA_PARAMS'] = {
    'host':'localhost',      #amqp.server.com
    'port': 5672,            #amqp server port
    'username' : 'guest',
    'password' : 'guest',
    'virtual_host':'vhost'   #amqp vhost
}


app.config['FLASK_PIKA_POOL_PARAMS'] =  {
    'pool_size': 8,
    'pool_recycle': 600
}
fpika = FPika(app)


ch = fpika.channel()
ch.basic_publish(exchange='daemon',routing_key='routing_key',exchange_type = 'fanout')
fpika.return_channel(ch)

if __name__ == "__main__":
    app.run()

Could you please review this and tell me if this looks good or what is the problem here?

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.