Pika python异步发布者:如何通过控制台从用户发送数据?

 聆听最遥远的歌声 发布于 2023-01-19 13:42

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

基本上是发布者发布用户输入.

我无法找到如何使发布者实时接收用户输入的任何示例或提示.我是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()解决我的问题

谢谢

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有