zoukankan      html  css  js  c++  java
  • rabbitmq安装使用

    使用 http://www.open-open.com/lib/view/open1325131828249.html

    ubuntu:
    apt-get install erlang-nox
    sudo apt-get install rabbitmq-server
    启动
    /etc/init.d/rabbitmq-server start|stop|restart (模式)

    创建目录
    sudo rabbitmqctl add_vhost /pyhtest
    创建用户名
    sudo rabbitmqctl add_user pyh pyh1234
    设置用户权限
    sudo rabbitmqctl set_permissions -p /pyhtest pyh “.*” “.*” “.*”

    启动 sudo  ./bin/rabbitmq-server

    python 调用
    下载py-amqplib https://pypi.python.orgpypi?:action=display&name=amqplib
    sudo chmod 777 setup.py
    安装
    python setup.py install

    生产者

    from amqplib import client_0_8 as amqp
    import sys
    
    filelist=[]
    
    def readfiletolist(filename):
        file = open(filename)
        for line in file.readlines():
         line=line.strip('
    ')
         filelist.append(line)
    #     print "line" + line
    
    
    conn = amqp.Connection(host="localhost:5672", userid="guest", password="guest", virtual_host="/", insist=False)
    chan = conn.channel()
    
    #readfiletolist("1.txt")
    readfiletolist("/home/tanbo/stockdata/2014-06-27.txt")
    for i in filelist:
            #msg = amqp.Message(i)
    #msg = amsg = amqp.Message(sys.argv[1])
            msg = amqp.Message(i)
            msg.properties["delivery_mode"] = 2
            chan.basic_publish(msg,exchange="sorting_room",routing_key="jason")
    
    chan.close() 
    conn.close()
    

    消费者

    from amqplib import client_0_8 as amqp
    
    conn = amqp.Connection(host="localhost:5672", userid="guest", password="guest", virtual_host="/", insist=False)
    chan = conn.channel()
    
    chan.queue_declare(queue="po_box", durable=True, exclusive=False, auto_delete=False)
    chan.exchange_declare(exchange="sorting_room", type="direct", durable=True, auto_delete=False,)
    
    chan.queue_bind(queue="po_box", exchange="sorting_room", routing_key="jason")
    
    def recv_callback(msg):
        print 'Received: ' + msg.body + ' from channel #' + str(msg.channel.channel_id)
    
    chan.basic_consume(queue='po_box', no_ack=True, callback=recv_callback, consumer_tag="testtag")
    while True:
        chan.wait()
    chan.basic_cancel("testtag")
    
    
    chan.close()
    conn.close()
    
  • 相关阅读:
    hdu 2647 Reward
    hdu 2094 产生冠军
    hdu 3342 Legal or Not
    hdu 1285 确定比赛名次
    hdu 3006 The Number of set
    hdu 1429 胜利大逃亡(续)
    UVA 146 ID Codes
    UVA 131 The Psychic Poker Player
    洛谷 P2491消防 解题报告
    洛谷 P2587 [ZJOI2008]泡泡堂 解题报告
  • 原文地址:https://www.cnblogs.com/chengxin1982/p/3820759.html
Copyright © 2011-2022 走看看