gpt4 book ai didi

python - Pika python 异步发布者 : how to send data from user via console?

转载 作者:太空狗 更新时间:2023-10-30 01:38:15 26 4
gpt4 key购买 nike

我正在使用标准的异步发布者示例。我注意到发布者将永远循环发布相同的消息。所以我评论了来自 publish_message 的 schedule_next_message 调用以停止该循环。但我真正想要的是发布者只有在用户给它一个“message_body”和“Key”时才开始发布

基本上发布者发布用户输入。

我无法找到任何示例或提示来说明如何让发布者实时获取用户的输入。我是 raabitmq、pika、python 等的新手

这是我正在谈论的代码片段:-

def publish_message(self):
"""If the class is not stopping, publish a message to RabbitMQ,
appending a list of deliveries with the message number that was sent.
This list will be used to check for delivery confirmations in the
on_delivery_confirmations method.

Once the message has been sent, schedule another message to be sent.
The main reason I put scheduling in was just so you can get a good idea
of how the process is flowing by slowing down and speeding up the
delivery intervals by changing the PUBLISH_INTERVAL constant in the
class.

"""
if self._stopping:
return

message = {"service":"sendgrid", "sender": "nutshi@gmail.com", "receiver": "nutshi@gmail.com", "subject": "test notification", "text":"sample email"}
routing_key = "email"
properties = pika.BasicProperties(app_id='example-publisher',
content_type='application/json',
headers=message)

self._channel.basic_publish(self.EXCHANGE, routing_key,
json.dumps(message, ensure_ascii=False),
properties)
self._message_number += 1
self._deliveries.append(self._message_number)
LOGGER.info('Published message # %i', self._message_number)
#self.schedule_next_message()
#self.stop()

def schedule_next_message(self):
"""If we are not closing our connection to RabbitMQ, schedule another
message to be delivered in PUBLISH_INTERVAL seconds.

"""
if self._stopping:
return
LOGGER.info('Scheduling next message for %0.1f seconds',
self.PUBLISH_INTERVAL)
self._connection.add_timeout(self.PUBLISH_INTERVAL,
self.publish_message)

def start_publishing(self):
"""This method will enable delivery confirmations and schedule the
first message to be sent to RabbitMQ

"""
LOGGER.info('Issuing consumer related RPC commands')
self.enable_delivery_confirmations()
self.schedule_next_message()

该网站不允许我添加解决方案..我能够使用 raw_input() 解决我的问题

谢谢

最佳答案

我知道我回答这个问题有点晚了,但你有没有看过 this one

似乎比使用完整的异步发布者更符合您的需求。通常,您使用带有 Python 队列的那些在线程之间传递消息。

关于python - Pika python 异步发布者 : how to send data from user via console?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22254252/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com