文件“/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py”,第218行,在process_data_events中 引发异常.ConnectionClosed() pika.exceptions.ConnectionClosed
def main():
print('zmq', zmq.zmq_version(), zmq.pyzmq_version())
zctx = zmq.Context()
socket_rep = zctx.socket(zmq.REP)
socket_rep.bind('tcp://*:'+str(dragon_pb2.tcpport.Value('stage1_server_request_storage')))
RabbitReceiver(host='localhost', on_receive_callback=handle_massage, exchange_name='exchange1', queue_name='name1')
while True:
try:
request = socket_rep.recv_json()
status, result = get_records(request)
if result is None:
result = {}
# print result
socket_rep.send_multipart([status, blosc.compress(json.dumps(result), 9)])
except KeyboardInterrupt:
print("Buy")
sys.exit()
if __name__ == '__main__':
main()
class RabbitReceiver(AbstractReceiver):
def __init__(self, on_receive_callback, host, exchange_name, queue_name):
AbstractReceiver.__init__(self, host=host, on_receive_callback=on_receive_callback)
connection = pika.BlockingConnection(pika.ConnectionParameters(
host=AbstractReceiver.host))
channel = connection.channel()
channel.exchange_declare(exchange=exchange_name,
type='fanout')
channel.queue_declare(queue=queue_name, durable=True)
channel.queue_bind(exchange=exchange_name,
queue=queue_name)
channel.basic_consume(self.on_message_receive,
queue=queue_name)
channel.start_consuming()
def on_message_receive(self, channel, method, header, body):
AbstractReceiver.on_message_receive(self, body)
channel.basic_ack(delivery_tag = method.delivery_tag)
我知道创建RabbitReceiver后的代码可能无法执行,因为start_consuming正在阻塞。但是现在我并不担心。 我想知道为什么它会给我这个错误? 可能我错过了一些东西......