使用 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('\n') 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()
?
?
?
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
