python stomp activemq mess send message in loop
To send messages to an ActiveMQ broker using the STOMP protocol in a loop with Python, you can use the stomp.py
library. First, you’ll need to ensure the library is installed. You can install it using pip if you haven’t already:
pip install stomp.py
Here’s a simple example of how to send messages in a loop:
import stomp
import time
class MyListener(stomp.ConnectionListener):
def on_error(self, headers, message):
print('received an error: "%s"' % message)
def on_disconnected(self):
print('disconnected')
def send_messages():
conn = stomp.Connection([('localhost', 61613)])
conn.set_listener('', MyListener())
conn.connect('admin', 'password', wait=True)
for i in range(10): # Send 10 messages
message = f'Message number {i + 1}'
conn.send(destination='/queue/test', body=message)
print(f'Sent: {message}')
time.sleep(1)
conn.disconnect()
if __name__ == '__main__':
send_messages()
'localhost'
and 61613
with your ActiveMQ server’s host and port.'admin'
and 'password'
with your ActiveMQ username and password.destination='/queue/test'
to the queue you want to send messages to.time.sleep()
for the number of messages and delay between sends as needed.This script establishes a connection to the ActiveMQ broker, sends a series of messages to a specified queue, and then disconnects.
GPT 的回答基本上解决了问题。
遇到的问题是,在向 ActiveMQ 发送消息的时候,我们有一个 SendMQ 的方法。
在这个方法将会打开连接发送消息后关闭连接。
我们的问题在:现在需要向 MQ 发送 1 万多条消息,这个循环放那里。
开始我们把这个循环放在了内层,这里就出现了一个问题,Python 的循环会异步调用 Close 这方法,然后导致整个程序的挂起。
通过上面 GPT 的回答,我知道应该在整个循环的最外层打开连接,循环内不能再次打开连接和关闭连接。
等把这 1 万多条消息发送成功后再关闭连接。
同样的问题,我们也使用了 claude_3_haiku_bot 这个模型来问了这个问题,请参考后面的消息。
感觉 GPT4 的模型对这个问题回答得更好一些。
Let’s see what topics on “python stomp sending in loop” I can find on the iSharkFly forum:
Search
Hmm, I couldn’t find any specific topics about “python stomp sending in loop” on this forum. Let me try a more general search:
Search
The search results provide some relevant information about using the Python stomp
library to send messages, including:
auto_content_length
setting. Setting this to False
resolved the issue.stomp
, send a message, and disconnect.Based on this, it seems the main issue you are encountering is getting the messages to display correctly on the message server. The key things to try are:
auto_content_length=False
when creating the stomp
connection.If you are still having issues, you could try searching for more specific information about the “python stomp sending in loop” use case, or providing more details about the exact problem you are facing.
Google AI 的回复貌似也还行。
关于写代码,真的可以问问 AI。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有