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()
    
  • 相关阅读:
    数据库规范化与经典三范式
    左右两个下拉框里的内容互换
    利用创造元素的方法进行下拉框内容的添加
    数组除重和应用随机数进行随机点名
    for 语句练习
    super和this
    linux常用命令
    g​e​t​A​t​t​r​i​b​u​t​e​和​g​e​t​P​a​r​a​m​e​t​e​r​区​别
    HTTP 方法:GET 对比 POST
    sql语句中字符串类型的变量前后需要使用单引号
  • 原文地址:https://www.cnblogs.com/chengxin1982/p/3820759.html
Copyright © 2011-2022 走看看