码迷,mamicode.com
首页 > 其他好文 > 详细

RabbitMQ

时间:2018-06-13 21:08:07      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:mes   IV   wait   blocking   线程   received   pre   python3   import   

RabbitMQ:
和python queue的区别
线程queue,多个线程之间数据同步用
进程queue,用于父进程与子进程交互,或同一父进程下多个子进程之间交互
rabbitmq,两个独立进程之间交互

依赖erlang

producer
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# -*- Author:Hinimix -*-

import pika

creditials = pika.PlainCredentials(admin, Inspiry2018)
conn = pika.BlockingConnection(
    pika.ConnectionParameters("192.168.20.61", 5672, /, creditials)
)

channel = conn.channel()    #声明一个管道

channel.queue_declare(queue=hinimix)

channel.basic_publish(
    exchange=‘‘,            #
    routing_key=hinimix,    #queue名字
    body=hello, world     #内容
)

print("[x] sent ‘hello world")

conn.close()

#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# -*- Author:Hinimix -*-

import pika

creditials = pika.PlainCredentials(admin, admin)
conn = pika.BlockingConnection(
    pika.ConnectionParameters("192.168.20.61", 5672, /, creditials)
)

channel = conn.channel()    #声明一个管道

channel.queue_declare(queue=hinimix)

channel.basic_publish(
    exchange=‘‘,            #
    routing_key=hinimix,    #queue名字
    body=hello, world     #内容
)

print("[x] sent ‘hello world")

conn.close()


consumer

 

#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# -*- Author:Hinimix -*-

import pika

creditials = pika.PlainCredentials(admin, admin)
conn = pika.BlockingConnection(
    pika.ConnectionParameters("192.168.20.61", 5672, /, creditials)
)

channel = conn.channel()
channel.queue_declare(queue=hinimix)


def callback(ch, method, properties, body):
    print(ch, @@, method,@@, properties,@@, body)
    print("[x] received %r" % body)

channel.basic_consume(
    callback,           # 如果收到消息调用callback来处理消息
    queue=hinimix,
    no_ack=True
)

print("[*] waiting for messages.")
channel.start_consuming()

 

RabbitMQ

标签:mes   IV   wait   blocking   线程   received   pre   python3   import   

原文地址:https://www.cnblogs.com/hinimix/p/9179535.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!