Sharing an object between Gunicorn workers or saving an object inside a worker

I am writing a WSGI application using the Nginx / Gunicorn / Bottle stack that accepts a GET request, returns a simple response, and then writes the message to RabbitMQ. If I run the application through a direct bottle, I would reuse the RabbitMQ connection every time the application receives a GET. However, at Gunicorn, it seems that workers destroy and recreate the MQ mix every time. I was wondering if there is a good way to reuse this connection.

More detailed information:

##This is my bottle app
from bottle import blahblahblah
import bottle
from mqconnector import MQConnector

mqc = MQConnector(ip, exchange)

@route('/')
def index():
  try:
    mqc
  except NameError:
    mqc = MQConnector(ip, exchange)

  mqc.publish('whatever message')
  return 'ok'

if __name__ == '__main__':
  run(host='blah', port=808)
app = bottle.default_app()
+7
source share
3 answers

, , . , , , , Gunicorn index() , , MQConnector.

MQConnector , , , . , MQConnector, MQConnector. , MQConnector publish().

#Bottle app
from blah import blahblah
import MQConnector

@route('/')
def index():
  blahblah(foo, bar, baz, MQConnector.publish)

#MQConnector
import pika
mq_ip = "blah"
exhange_name="blahblah"

connection=pika.BlockingConnection(....
...

def publish(message, r_key):
  ...

: , 800 , 4 . 80 90 Gunicorn, 700 5 Gunicorn.

+6

,

MQConnector

- ? MQConnector ?

0

How to keep a connection blocking pika for future requests? I mean, if an employee has not processed any request for the amount of time X, the pika connection may be automatically closed, so this will throw an exception when the next request arrives.

Is there a solution to solve this problem?

-1
source

All Articles