Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pika python asynchronous publisher: how to send data from user via console?

I am using the standard asynchronous publisher example. and i noticed that the publisher will keep publishing the same message in a loop forever. So i commented the schedule_next_message call from publish_message to stop that loop. But what i really want is for the publissher to start and publish only when a user give it a "message_body" and "Key"

basically publisher to publish the user inputs.

i was not able to fin any examples or hints of how to make the publisher take inputs from user in real time. I am new to raabitmq, pika, python e.t.c

here is the snippet of code i am talking about :-

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": "[email protected]", "receiver": "[email protected]", "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()

the site does not let me add the solution .. i was able to solve my issue using raw_input()

Thanks

like image 331
user2574872 Avatar asked Nov 23 '22 02:11

user2574872


1 Answers

I know I'm a bit late to answer the question but have you looked at this one?

Seems to be a bit more related to what you need than using a full async publisher. Normally you use those with a Python Queue to pass messages between threads.

like image 57
ZeroSoul13 Avatar answered May 18 '23 12:05

ZeroSoul13