zoukankan      html  css  js  c++  java
  • python rabittmq 使用

    Reference: https://www.rabbitmq.com/tutorials/tutorial-three-python.html

    Publish/Subscribe

    (using the pika 0.10.0 Python client)

    Prerequisites

    This tutorial assumes RabbitMQ is installedand running on localhost on standard port (5672). In case you use a different host, port or credentials, connections settings would require adjusting.

    Where to get help

    If you're having trouble going through this tutorial you can contact us through the mailing list.

    In the previous tutorial we created a work queue. The assumption behind a work queue is that each task is delivered to exactly one worker. In this part we'll do something completely different -- we'll deliver a message to multiple consumers. This pattern is known as "publish/subscribe".

    To illustrate the pattern, we're going to build a simple logging system. It will consist of two programs -- the first will emit log messages and the second will receive and print them.

    In our logging system every running copy of the receiver program will get the messages. That way we'll be able to run one receiver and direct the logs to disk; and at the same time we'll be able to run another receiver and see the logs on the screen.

    Essentially, published log messages are going to be broadcast to all the receivers.

    Exchanges

    In previous parts of the tutorial we sent and received messages to and from a queue. Now it's time to introduce the full messaging model in Rabbit.

    Let's quickly go over what we covered in the previous tutorials:

    • producer is a user application that sends messages.
    • queue is a buffer that stores messages.
    • consumer is a user application that receives messages.

    The core idea in the messaging model in RabbitMQ is that the producer never sends any messages directly to a queue. Actually, quite often the producer doesn't even know if a message will be delivered to any queue at all.

    Instead, the producer can only send messages to an exchange. An exchange is a very simple thing. On one side it receives messages from producers and the other side it pushes them to queues. The exchange must know exactly what to do with a message it receives. Should it be appended to a particular queue? Should it be appended to many queues? Or should it get discarded. The rules for that are defined by the exchange type.

    There are a few exchange types available: direct, topic, headers and fanout. We'll focus on the last one -- the fanout. Let's create an exchange of that type, and call it logs:

    channel.exchange_declare(exchange='logs',
                             type='fanout')
    

    The fanout exchange is very simple. As you can probably guess from the name, it just broadcasts all the messages it receives to all the queues it knows. And that's exactly what we need for our logger.

    Listing exchanges

    To list the exchanges on the server you can run the ever useful rabbitmqctl:

    $ sudo rabbitmqctl list_exchanges
    Listing exchanges ...
    logs      fanout
    amq.direct      direct
    amq.topic       topic
    amq.fanout      fanout
    amq.headers     headers
    ...done.
    

    In this list there are some amq.* exchanges and the default (unnamed) exchange. These are created by default, but it is unlikely you'll need to use them at the moment.

    Nameless exchange

    In previous parts of the tutorial we knew nothing about exchanges, but still were able to send messages to queues. That was possible because we were using a default exchange, which we identify by the empty string ("").

    Recall how we published a message before:

    channel.basic_publish(exchange='',
                          routing_key='hello',
                          body=message)
    

    The exchange parameter is the name of the exchange. The empty string denotes the default or namelessexchange: messages are routed to the queue with the name specified by routing_key, if it exists.

    Now, we can publish to our named exchange instead:

    channel.basic_publish(exchange='logs',
                          routing_key='',
                          body=message)
    

    Temporary queues

    As you may remember previously we were using queues which had a specified name (remember hello andtask_queue?). Being able to name a queue was crucial for us -- we needed to point the workers to the same queue. Giving a queue a name is important when you want to share the queue between producers and consumers.

    But that's not the case for our logger. We want to hear about all log messages, not just a subset of them. We're also interested only in currently flowing messages not in the old ones. To solve that we need two things.

    Firstly, whenever we connect to Rabbit we need a fresh, empty queue. To do it we could create a queue with a random name, or, even better - let the server choose a random queue name for us. We can do this by not supplying the queue parameter to queue_declare:

    result = channel.queue_declare()
    

    At this point result.method.queue contains a random queue name. For example it may look likeamq.gen-JzTY20BRgKO-HjmUJj0wLg.

    Secondly, once we disconnect the consumer the queue should be deleted. There's an exclusive flag for that:

    result = channel.queue_declare(exclusive=True)
    

    Bindings

    We've already created a fanout exchange and a queue. Now we need to tell the exchange to send messages to our queue. That relationship between exchange and a queue is called a binding.

    channel.queue_bind(exchange='logs',
                       queue=result.method.queue)
    

    From now on the logs exchange will append messages to our queue.

    Listing bindings

    You can list existing bindings using, you guessed it, rabbitmqctl list_bindings.

    Putting it all together

    The producer program, which emits log messages, doesn't look much different from the previous tutorial. The most important change is that we now want to publish messages to our logs exchange instead of the nameless one. We need to supply a routing_key when sending, but its value is ignored for fanout exchanges. Here goes the code for emit_log.py script:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    #!/usr/bin/env python
    import pika
    import sys
    
    connection = pika.BlockingConnection(pika.ConnectionParameters(
            host='localhost'))
    channel = connection.channel()
    
    channel.exchange_declare(exchange='logs',
                             type='fanout')
    
    message = ' '.join(sys.argv[1:]) or "info: Hello World!"
    channel.basic_publish(exchange='logs',
                          routing_key='',
                          body=message)
    print(" [x] Sent %r" % message)
    connection.close()
    

    (emit_log.py source)

    As you see, after establishing the connection we declared the exchange. This step is necessary as publishing to a non-existing exchange is forbidden.

    The messages will be lost if no queue is bound to the exchange yet, but that's okay for us; if no consumer is listening yet we can safely discard the message.

    The code for receive_logs.py:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    #!/usr/bin/env python
    import pika
    
    connection = pika.BlockingConnection(pika.ConnectionParameters(
            host='localhost'))
    channel = connection.channel()
    
    channel.exchange_declare(exchange='logs',
                             type='fanout')
    
    result = channel.queue_declare(exclusive=True)
    queue_name = result.method.queue
    
    channel.queue_bind(exchange='logs',
                       queue=queue_name)
    
    print(' [*] Waiting for logs. To exit press CTRL+C')
    
    def callback(ch, method, properties, body):
        print(" [x] %r" % body)
    
    channel.basic_consume(callback,
                          queue=queue_name,
                          no_ack=True)
    
    channel.start_consuming()
    

    (receive_logs.py source)

    We're done. If you want to save logs to a file, just open a console and type:

    $ python receive_logs.py > logs_from_rabbit.log
    

    If you wish to see the logs on your screen, spawn a new terminal and run:

    $ python receive_logs.py
    

    And of course, to emit logs type:

    $ python emit_log.py
    

    Using rabbitmqctl list_bindings you can verify that the code actually creates bindings and queues as we want. With two receive_logs.py programs running you should see something like:

    $ sudo rabbitmqctl list_bindings
    Listing bindings ...
    logs    exchange        amq.gen-JzTY20BRgKO-HjmUJj0wLg  queue           []
    logs    exchange        amq.gen-vso0PVvyiRIL2WoV3i48Yg  queue           []
    ...done.
    

    The interpretation of the result is straightforward: data from exchange logs goes to two queues with server-assigned names. And that's exactly what we intended.

    To find out how to listen for a subset of messages, let's move on to tutorial 4

  • 相关阅读:
    NTP服务编译安装报错:/usr/bin/ld: cannot find –lcap
    NTP服务编译安装报错:ntpd.c:124:29: 致命错误:sys/capability.h:没有那个文件或目录
    yum运行报错:/usr/lib64/python2.7/site-packages/pycurl.so: undefined symbol: CRYPTO_num_locks
    yum运行报错:libcurl.so.4: cannot open shared object file: No such file or directory
    Xftp工具连接 报错: 无法与 “ 目标IP ” 连接
    yum运行报错:File "/usr/libexec/urlgrabber-ext-down", line 28
    webkit content-type missing in HTTP
    快速排序算法中选择“基准“点的方法
    数据结构与算法--归并排序
    数据结构与算法--拓扑排序
  • 原文地址:https://www.cnblogs.com/skying555/p/6530697.html
Copyright © 2011-2022 走看看