Stream connection lost: AssertionError(('_AsyncTransportBase._produce() tx buffer size underflow', -275, 1),)

Viewed 1014

This error occurs when many requests are made to be published

error: Stream connection lost: AssertionError(('_AsyncTransportBase._produce() tx buffer size underflow', -275, 1),)

@name_space.route('/gravar-coleta-lista-hoteis')
class GravarColetaListaHoteis(Resource):
    def post(self):
        try:
            data = request.get_json()
            rabbitMQProducerService.publish(data, QueueNameConst.queue_name_hotel)
            return "ok"
class RabbitMQProducerService:
connection = None
channel = None
persistence_dao_mongo = None
rabbitmq_host = None
credentials = None

def __init__(self, rabbitmq_host, rabbitmq_user, rabbitmq_password):
    self.rabbitmq_host = rabbitmq_host
    self.credentials = pika.PlainCredentials(rabbitmq_user, rabbitmq_password)
    self.connect()

def publish(self, body, queue_name):
    if self.channel.is_closed:
        self.connect()
    self.channel.basic_publish(exchange='',
                               routing_key=queue_name,
                               body=json.dumps(body))

def connect(self):
    self.connection = pika.BlockingConnection(pika.ConnectionParameters(
        host=self.rabbitmq_host,
        credentials=self.credentials))
    self.channel = self.connection.channel()
2 Answers

My solution was to create two instances of my class Queue or your class RabbitMQProducerService with an init variable of listener: bool = True where one was True and the other False to be able to send data in both directions since pika isn't safe with threading as said by Syed in the comments of the question.

This is because of threading. Use conn.connection.add_callback_threadsafe

def callback_publish(ch):
    ch.basic_publish(exchange='',
                     routing_key='foo',
                     body='{"response": "success"}')


cb = functools.partial(callback_publish, ch)
conn.connection.add_callback_threadsafe(cb)
Related