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
生产者
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 27 | 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() |
消费者
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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() |
分类:
rabbitmq
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!