def queue_setup(queue): connection = get_rabbit_connection() channel = connection.channel() channel.queue_declare(queue=queue) channel.basic_consume(queue=queue, on_message_callback=queue_callback, auto_ack=True) channel.start_consuming()
def producter(message): # 消息生产者 # 获取与rabbitmq 服务的连接,虚拟队列需要指定参数 virtual_host,如果是默认的可以不填(默认为/),也可以自己创建一个 connection = pika.BlockingConnection( pika.ConnectionParameters(host='192.168.25.128', port=5672, credentials=pika.PlainCredentials('guest', 'guest'))) # 创建一个 AMQP 信道(Channel),建造一个大邮箱,隶属于这家邮局的邮箱 channel = connection.channel() # 声明消息队列tester,消息将在这个队列传递,如不存在,则创建 channel.queue_declare(queue='tester') # 向队列插入数值 routing_key的队列名为tester,body 就是放入的消息内容,exchange指定消息在哪个队列传递,这里是空的exchange但仍然能够发送消息到队列中,因为我们使用的是我们定义的空字符串“”exchange(默认的exchange) channel.basic_publish(exchange='', routing_key='tester', body=message) # 关闭连接 connection.close()
def save_output(queue, ids, outputs, type='SAVE'): connection = get_rabbit_connection() channel = connection.channel() for id, output in zip (ids, outputs): message = { 'id': id, 'output': output, 'type': type } channel.basic_publish(exchange='', routing_key=queue, body=json.dumps(message))
def main(): connection = pika.BlockingConnection( pika.ConnectionParameters(host='localhost')) channel = connection.channel() channel.queue_declare(queue='hello') def callback(ch, method, properties, body): print(' [x] Received %r' % body) channel.basic_consume(queue='hello', on_message_callback=callback, auto_ack=True) print(' [*] Waiting for messages. To exit press CTRC+C') channel.start_consuming()
def main(): url = 'amqps://*****:*****@fish.rmq.cloudamqp.com/kyiirjzl' parametros = pika.URLParameters(url) connection = pika.BlockingConnection(parametros) channel = connection.channel() channel.queue_declare(queue='Queue_Data') def callback(ch, method, properties, body): print(body.decode()) channel.basic_consume(queue='Queue_Data', auto_ack=True, on_message_callback=callback) channel.start_consuming()
def main(): connection = pika.BlockingConnection( pika.ConnectionParameters(host='localhost')) channel = connection.channel() channel.queue_declare(queue='task_queue', durable=True) def callback(ch, method, properties, body): print(' [x] Received %r' % body.decode()) time.sleep(body.count(b'.')) print(' [x] Done') ch.basic_ack(delivery_tag=method.delivery_tag) channel.basic_qos(prefetch_count=1) channel.basic_consume(queue='task_queue', on_message_callback=callback) print(' [*] Waiting for messages. To exit press CTRC+C') channel.start_consuming()
def create_channel(self, connection): return connection.channel()
import pika from pika import connection from pika import channel import json params = pika.URLParameters( "amqps://*****:*****@hornet.rmq.cloudamqp.com/lrtmtglx" ) connection = pika.BlockingConnection(params) channel = connection.channel() def publish(method, body): properties = pika.BasicProperties(method) channel.basic_publish(exchange="", routing_key="main", body=json.dumps(body), properties=properties)
import pika import pika.connection import pika.credentials as pika_credentials connection = pika.BlockingConnection(pika.ConnectionParameters('contrail.rabbitmq.uk2.prod.skyscanner.local',5672, 'contrail', socket_timeout=300,credentials=pika_credentials.PlainCredentials('svc_travelrankings', 'svC_7R4Nsp0R7_S7472'))) connection.channel() print connection