<div dir="ltr">The connect method doesn&#39;t apear to return a connection...</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Aug 15, 2013 at 9:51 AM, Priyanki Vashi <span dir="ltr">&lt;<a href="mailto:vashi.priyanki@gmail.com" target="_blank">vashi.priyanki@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi ,<div><br></div><div>on_connection_open, is a callback method, which will be called when connection is opened with rabbitmq. It opens channel inturn. So it&#39;s little unlikely that it has &#39;None&#39; value but I am not 100% sure. </div>


<div><br></div><div>Here is my code. Could you quickly have a look.</div><div>It&#39;s the same example from pika site for tornado consumer except that I have tried to use connection parameters instead of URL.</div><div>

<br></div><div><br></div><div><div>from pika import adapters</div>
<div>import pika</div><div>import logging</div><div><br></div><div>LOG_FORMAT = (&#39;%(levelname) -10s %(asctime)s %(name) -30s %(funcName) &#39;</div><div>              &#39;-35s %(lineno) -5d: %(message)s&#39;)</div><div>


LOGGER = logging.getLogger(__name__)</div><div><br></div><div><br></div><div>class ExampleConsumer(object):</div><div>    &quot;&quot;&quot;This is an example consumer that will handle unexpected interactions</div><div>    with RabbitMQ such as channel and connection closures.</div>


<div><br></div><div>    If RabbitMQ closes the connection, it will reopen it. You should</div><div>    look at the output, as there are limited reasons why the connection may</div><div>    be closed, which usually are tied to permission related issues or</div>


<div>    socket timeouts.</div><div><br></div><div>    If the channel is closed, it will indicate a problem with one of the</div><div>    commands that were issued and that should surface in the output as well.</div><div>


<br></div><div>    &quot;&quot;&quot;</div><div>    EXCHANGE = &#39;message&#39;</div><div>    EXCHANGE_TYPE = &#39;topic&#39;</div><div>    QUEUE = &#39;text&#39;</div><div>    ROUTING_KEY = &#39;example.text&#39;</div>

<div>
<br></div><div>   </div><div>       </div><div><br></div><div>    def __init__(self, host, port, username, password):</div><div>        &quot;&quot;&quot;Setup the example publisher object, passing in the URL we will use</div>


<div>        to connect to RabbitMQ.</div><div><br></div><div>        :param str amqp_url: The URL for connecting to RabbitMQ</div><div><br></div><div>        &quot;&quot;&quot;</div><div>        </div><div>        self._connection = None</div>


<div>        self._channel = None</div><div>        self._closing = False</div><div>        self._consumer_tag = None</div><div>        #self._url = amqp_url</div><div>        </div><div>        self.credentials = pika.PlainCredentials(username=username, password=password)</div>


<div>        self.parameters = pika.ConnectionParameters(host=host, port=port, credentials=self.credentials)</div><div>     </div><div><br></div><div><br></div><div>    def connect(self):</div><div>        &quot;&quot;&quot;This method connects to RabbitMQ, returning the connection handle.</div>


<div>        When the connection is established, the on_connection_open method</div><div>        will be invoked by pika.</div><div><br></div><div>        :rtype: pika.SelectConnection</div><div><br></div><div>        &quot;&quot;&quot;</div>


<div>        #LOGGER.info(&#39;Connecting to %s&#39;, self._url)</div><div>        adapters.TornadoConnection(self.parameters, on_open_callback=self.on_connection_open, stop_ioloop_on_close=False)</div><div>        #return adapters.TornadoConnection(pika.URLParameters(self._url),self.on_connection_open)</div>


<div><br></div><div>    def close_connection(self):</div><div>        &quot;&quot;&quot;This method closes the connection to RabbitMQ.&quot;&quot;&quot;</div><div>        LOGGER.info(&#39;Closing connection&#39;)</div><div>


        self._connection.close()</div><div><br></div><div>    def add_on_connection_close_callback(self):</div><div>        &quot;&quot;&quot;This method adds an on close callback that will be invoked by pika</div><div>        when RabbitMQ closes the connection to the publisher unexpectedly.</div>


<div><br></div><div>        &quot;&quot;&quot;</div><div>        LOGGER.info(&#39;Adding connection close callback&#39;)</div><div>        self._connection.add_on_close_callback(self.on_connection_closed)</div><div><br></div>


<div>    def on_connection_closed(self, connection, reply_code, reply_text):</div><div>        &quot;&quot;&quot;This method is invoked by pika when the connection to RabbitMQ is</div><div>        closed unexpectedly. Since it is unexpected, we will reconnect to</div>


<div>        RabbitMQ if it disconnects.</div><div><br></div><div>        :param pika.connection.Connection connection: The closed connection obj</div><div>        :param int reply_code: The server provided reply_code if given</div>


<div>        :param str reply_text: The server provided reply_text if given</div><div><br></div><div>        &quot;&quot;&quot;</div><div>        self._channel = None</div><div>        if self._closing:</div><div>            self._connection.ioloop.stop()</div>


<div>        else:</div><div>            LOGGER.warning(&#39;Connection closed, reopening in 5 seconds: (%s) %s&#39;,</div><div>                           reply_code, reply_text)</div><div>            self._connection.add_timeout(5, self.reconnect)</div>


<div><br></div><div>    def on_connection_open(self, unused_connection):</div><div>        &quot;&quot;&quot;This method is called by pika once the connection to RabbitMQ has</div><div>        been established. It passes the handle to the connection object in</div>


<div>        case we need it, but in this case, we&#39;ll just mark it unused.</div><div><br></div><div>        :type unused_connection: pika.SelectConnection</div><div><br></div><div>        &quot;&quot;&quot;</div><div>


        LOGGER.info(&#39;Connection opened&#39;)</div><div>        self.add_on_connection_close_callback()</div><div>        self.open_channel()</div><div><br></div><div>    def reconnect(self):</div><div>        &quot;&quot;&quot;Will be invoked by the IOLoop timer if the connection is</div>


<div>        closed. See the on_connection_closed method.</div><div><br></div><div>        &quot;&quot;&quot;</div><div>        # This is the old connection IOLoop instance, stop its ioloop</div><div>        self._connection.ioloop.stop()</div>


<div><br></div><div>        if not self._closing:</div><div><br></div><div>            # Create a new connection</div><div>            self._connection = self.connect()</div><div><br></div><div>            # There is now a new connection, needs a new ioloop to run</div>


<div>            self._connection.ioloop.start()</div><div><br></div><div>    def add_on_channel_close_callback(self):</div><div>        &quot;&quot;&quot;This method tells pika to call the on_channel_closed method if</div>


<div>        RabbitMQ unexpectedly closes the channel.</div><div><br></div><div>        &quot;&quot;&quot;</div><div>        LOGGER.info(&#39;Adding channel close callback&#39;)</div><div>        self._channel.add_on_close_callback(self.on_channel_closed)</div>


<div><br></div><div>    def on_channel_closed(self, channel, reply_code, reply_text):</div><div>        &quot;&quot;&quot;Invoked by pika when RabbitMQ unexpectedly closes the channel.</div><div>        Channels are usually closed if you attempt to do something that</div>


<div>        violates the protocol, such as re-declare an exchange or queue with</div><div>        different parameters. In this case, we&#39;ll close the connection</div><div>        to shutdown the object.</div><div><br>


</div><div>        :param pika.channel.Channel: The closed channel</div><div>        :param int reply_code: The numeric reason the channel was closed</div><div>        :param str reply_text: The text reason the channel was closed</div>


<div><br></div><div>        &quot;&quot;&quot;</div><div>        LOGGER.warning(&#39;Channel %i was closed: (%s) %s&#39;,</div><div>                       channel, reply_code, reply_text)</div><div>        self._connection.close()</div>


<div><br></div><div>    def on_channel_open(self, channel):</div><div>        &quot;&quot;&quot;This method is invoked by pika when the channel has been opened.</div><div>        The channel object is passed in so we can make use of it.</div>


<div><br></div><div>        Since the channel is now open, we&#39;ll declare the exchange to use.</div><div><br></div><div>        :param pika.channel.Channel channel: The channel object</div><div><br></div><div>        &quot;&quot;&quot;</div>


<div>        LOGGER.info(&#39;Channel opened&#39;)</div><div>        self._channel = channel</div><div>        self.add_on_channel_close_callback()</div><div>        self.setup_exchange(self.EXCHANGE)</div><div><br></div>


<div>    def setup_exchange(self, exchange_name):</div><div>        &quot;&quot;&quot;Setup the exchange on RabbitMQ by invoking the Exchange.Declare RPC</div><div>        command. When it is complete, the on_exchange_declareok method will</div>


<div>        be invoked by pika.</div><div><br></div><div>        :param str|unicode exchange_name: The name of the exchange to declare</div><div><br></div><div>        &quot;&quot;&quot;</div><div>        LOGGER.info(&#39;Declaring exchange %s&#39;, exchange_name)</div>


<div>        self._channel.exchange_declare(self.on_exchange_declareok,</div><div>                                       exchange_name,</div><div>                                       self.EXCHANGE_TYPE)</div><div><br></div>


<div>    def on_exchange_declareok(self, unused_frame):</div><div>        &quot;&quot;&quot;Invoked by pika when RabbitMQ has finished the Exchange.Declare RPC</div><div>        command.</div><div><br></div><div>        :param pika.Frame.Method unused_frame: Exchange.DeclareOk response frame</div>


<div><br></div><div>        &quot;&quot;&quot;</div><div>        LOGGER.info(&#39;Exchange declared&#39;)</div><div>        self.setup_queue(self.QUEUE)</div><div><br></div><div>    def setup_queue(self, queue_name):</div>


<div>        &quot;&quot;&quot;Setup the queue on RabbitMQ by invoking the Queue.Declare RPC</div><div>        command. When it is complete, the on_queue_declareok method will</div><div>        be invoked by pika.</div><div>


<br></div><div>        :param str|unicode queue_name: The name of the queue to declare.</div><div><br></div><div>        &quot;&quot;&quot;</div><div>        LOGGER.info(&#39;Declaring queue %s&#39;, queue_name)</div><div>


        self._channel.queue_declare(self.on_queue_declareok, queue_name)</div><div><br></div><div>    def on_queue_declareok(self, method_frame):</div><div>        &quot;&quot;&quot;Method invoked by pika when the Queue.Declare RPC call made in</div>


<div>        setup_queue has completed. In this method we will bind the queue</div><div>        and exchange together with the routing key by issuing the Queue.Bind</div><div>        RPC command. When this command is complete, the on_bindok method will</div>


<div>        be invoked by pika.</div><div><br></div><div>        :param pika.frame.Method method_frame: The Queue.DeclareOk frame</div><div><br></div><div>        &quot;&quot;&quot;</div><div>        LOGGER.info(&#39;Binding %s to %s with %s&#39;,</div>


<div>                    self.EXCHANGE, self.QUEUE, self.ROUTING_KEY)</div><div>        self._channel.queue_bind(self.on_bindok, self.QUEUE,</div><div>                                 self.EXCHANGE, self.ROUTING_KEY)</div>


<div><br></div><div>    def add_on_cancel_callback(self):</div><div>        &quot;&quot;&quot;Add a callback that will be invoked if RabbitMQ cancels the consumer</div><div>        for some reason. If RabbitMQ does cancel the consumer,</div>


<div>        on_consumer_cancelled will be invoked by pika.</div><div><br></div><div>        &quot;&quot;&quot;</div><div>        LOGGER.info(&#39;Adding consumer cancellation callback&#39;)</div><div>        self._channel.add_on_cancel_callback(self.on_consumer_cancelled)</div>


<div><br></div><div>    def on_consumer_cancelled(self, method_frame):</div><div>        &quot;&quot;&quot;Invoked by pika when RabbitMQ sends a Basic.Cancel for a consumer</div><div>        receiving messages.</div><div>


<br></div><div>        :param pika.frame.Method method_frame: The Basic.Cancel frame</div><div><br></div><div>        &quot;&quot;&quot;</div><div>        LOGGER.info(&#39;Consumer was cancelled remotely, shutting down: %r&#39;,</div>


<div>                    method_frame)</div><div>        if self._channel:</div><div>            self._channel.close()</div><div><br></div><div>    def acknowledge_message(self, delivery_tag):</div><div>        &quot;&quot;&quot;Acknowledge the message delivery from RabbitMQ by sending a</div>


<div>        Basic.Ack RPC method for the delivery tag.</div><div><br></div><div>        :param int delivery_tag: The delivery tag from the Basic.Deliver frame</div><div><br></div><div>        &quot;&quot;&quot;</div><div>


        LOGGER.info(&#39;Acknowledging message %s&#39;, delivery_tag)</div><div>        self._channel.basic_ack(delivery_tag)</div><div><br></div><div>    def on_message(self, unused_channel, basic_deliver, properties, body):</div>


<div>        &quot;&quot;&quot;Invoked by pika when a message is delivered from RabbitMQ. The</div><div>        channel is passed for your convenience. The basic_deliver object that</div><div>        is passed in carries the exchange, routing key, delivery tag and</div>


<div>        a redelivered flag for the message. The properties passed in is an</div><div>        instance of BasicProperties with the message properties and the body</div><div>        is the message that was sent.</div>

<div>
<br></div><div>        :param pika.channel.Channel unused_channel: The channel object</div><div>        :param pika.Spec.Basic.Deliver: basic_deliver method</div><div>        :param pika.Spec.BasicProperties: properties</div>


<div>        :param str|unicode body: The message body</div><div><br></div><div>        &quot;&quot;&quot;</div><div>        LOGGER.info(&#39;Received message # %s from %s: %s&#39;,</div><div>                    basic_deliver.delivery_tag, properties.app_id, body)</div>


<div>        self.acknowledge_message(basic_deliver.delivery_tag)</div><div><br></div><div>    def on_cancelok(self, unused_frame):</div><div>        &quot;&quot;&quot;This method is invoked by pika when RabbitMQ acknowledges the</div>


<div>        cancellation of a consumer. At this point we will close the channel.</div><div>        This will invoke the on_channel_closed method once the channel has been</div><div>        closed, which will in-turn close the connection.</div>


<div><br></div><div>        :param pika.frame.Method unused_frame: The Basic.CancelOk frame</div><div><br></div><div>        &quot;&quot;&quot;</div><div>        LOGGER.info(&#39;RabbitMQ acknowledged the cancellation of the consumer&#39;)</div>


<div>        self.close_channel()</div><div><br></div><div>    def stop_consuming(self):</div><div>        &quot;&quot;&quot;Tell RabbitMQ that you would like to stop consuming by sending the</div><div>        Basic.Cancel RPC command.</div>


<div><br></div><div>        &quot;&quot;&quot;</div><div>        if self._channel:</div><div>            LOGGER.info(&#39;Sending a Basic.Cancel RPC command to RabbitMQ&#39;)</div><div>            self._channel.basic_cancel(self.on_cancelok, self._consumer_tag)</div>


<div><br></div><div>    def start_consuming(self):</div><div>        &quot;&quot;&quot;This method sets up the consumer by first calling</div><div>        add_on_cancel_callback so that the object is notified if RabbitMQ</div>


<div>        cancels the consumer. It then issues the Basic.Consume RPC command</div><div>        which returns the consumer tag that is used to uniquely identify the</div><div>        consumer with RabbitMQ. We keep the value to use it when we want to</div>


<div>        cancel consuming. The on_message method is passed in as a callback pika</div><div>        will invoke when a message is fully received.</div><div><br></div><div>        &quot;&quot;&quot;</div><div>        LOGGER.info(&#39;Issuing consumer related RPC commands&#39;)</div>


<div>        self.add_on_cancel_callback()</div><div>        self._consumer_tag = self._channel.basic_consume(self.on_message,</div><div>                                                         self.QUEUE)</div><div><br>

</div>
<div>    def on_bindok(self, unused_frame):</div><div>        &quot;&quot;&quot;Invoked by pika when the Queue.Bind method has completed. At this</div><div>        point we will start consuming messages by calling start_consuming</div>


<div>        which will invoke the needed RPC commands to start the process.</div><div><br></div><div>        :param pika.frame.Method unused_frame: The Queue.BindOk response frame</div><div><br></div><div>        &quot;&quot;&quot;</div>


<div>        LOGGER.info(&#39;Queue bound&#39;)</div><div>        self.start_consuming()</div><div><br></div><div>    def close_channel(self):</div><div>        &quot;&quot;&quot;Call to close the channel with RabbitMQ cleanly by issuing the</div>


<div>        Channel.Close RPC command.</div><div><br></div><div>        &quot;&quot;&quot;</div><div>        LOGGER.info(&#39;Closing the channel&#39;)</div><div>        self._channel.close()</div><div><br></div><div>    def open_channel(self):</div>


<div>        &quot;&quot;&quot;Open a new channel with RabbitMQ by issuing the Channel.Open RPC</div><div>        command. When RabbitMQ responds that the channel is open, the</div><div>        on_channel_open callback will be invoked by pika.</div>


<div><br></div><div>        &quot;&quot;&quot;</div><div>        LOGGER.info(&#39;Creating a new channel&#39;)</div><div>        self._connection.channel(on_open_callback=self.on_channel_open)</div><div><br></div><div>    def run(self):</div>


<div>        &quot;&quot;&quot;Run the example consumer by connecting to RabbitMQ and then</div><div>        starting the IOLoop to block and allow the SelectConnection to operate.</div><div><br></div><div>        &quot;&quot;&quot;</div>


<div>        self._connection = self.connect()</div><div>        self._connection.ioloop.start()</div><div><br></div><div>    def stop(self):</div><div>        &quot;&quot;&quot;Cleanly shutdown the connection to RabbitMQ by stopping the consumer</div>


<div>        with RabbitMQ. When RabbitMQ confirms the cancellation, on_cancelok</div><div>        will be invoked by pika, which will then closing the channel and</div><div>        connection. The IOLoop is started again because this method is invoked</div>


<div>        when CTRL-C is pressed raising a KeyboardInterrupt exception. This</div><div>        exception stops the IOLoop which needs to be running for pika to</div><div>        communicate with RabbitMQ. All of the commands issued prior to starting</div>


<div>        the IOLoop will be buffered but not processed.</div><div><br></div><div>        &quot;&quot;&quot;</div><div>        LOGGER.info(&#39;Stopping&#39;)</div><div>        self._closing = True</div><div>        self.stop_consuming()</div>


<div>        self._connection.ioloop.start()</div><div>        LOGGER.info(&#39;Stopped&#39;)</div><div><br></div><div><br></div><div>def main():</div><div>    logging.basicConfig(level=logging.INFO, format=LOG_FORMAT)</div>


<div>    example = ExampleConsumer(&#39;mqserver10&#39;, 5672, &#39;guest&#39;, &#39;guest&#39;)</div><div>    try:</div><div>        example.run()</div><div>    except KeyboardInterrupt:</div><div>        example.stop()</div>


<div><br></div><div><br></div><div>if __name__ == &#39;__main__&#39;:</div><div>    main()</div></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Aug 15, 2013 at 3:25 PM, Ask Solem <span dir="ltr">&lt;<a href="mailto:ask@rabbitmq.com" target="_blank">ask@rabbitmq.com</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div><br>
On Aug 15, 2013, at 1:20 PM, Priyanki Vashi &lt;<a href="mailto:vashi.priyanki@gmail.com" target="_blank">vashi.priyanki@gmail.com</a>&gt; wrote:<br>
<br>
&gt; Hi There ,<br>
&gt;<br>
&gt; This might be very basic error but since I am trying to use Tornado connection for the first time, help on this would be appreciated.<br>
&gt;<br>
&gt; I was trying to use Tornado consumer example given on following pika site.<br>
&gt;<br>
&gt; <a href="https://pika.readthedocs.org/en/0.9.13/examples/tornado_consumer.html" target="_blank">https://pika.readthedocs.org/en/0.9.13/examples/tornado_consumer.html</a><br>
&gt;<br>
&gt; I am running into following error. The example is with URL connection method.<br>
&gt; But I also tried to use hostname and port method as well as tried changing to IP address instead of hostname but still it&#39;s the same error.<br>
&gt;<br>
&gt; I have pika 0.9.13 library installed. My consumer &amp; Producer works fine with select.connection method so I believe library is correctly installed.<br>
&gt;<br>
&gt; Do I need to do something additional to get tornado connection working for both producer and consumer with pika?<br>
&gt;<br>
&gt; Please suggest.<br>
&gt;<br>
&gt; mq1@mqserver1:~/Producer_Receiver/Latest$ python tornedo_c_1.py<br>
&gt; Traceback (most recent call last):<br>
&gt;   File &quot;tornedo_c_1.py&quot;, line 357, in &lt;module&gt;<br>
&gt;     main()<br>
&gt;   File &quot;tornedo_c_1.py&quot;, line 351, in main<br>
&gt;     example.run()<br>
&gt;   File &quot;tornedo_c_1.py&quot;, line 326, in run<br>
&gt;     self._connection = self.connect()<br>
&gt;   File &quot;tornedo_c_1.py&quot;, line 59, in connect<br>
&gt;     adapters.TornadoConnection(self.parameters, self.on_connection_open, stop_ioloop_on_close=False)<br>
&gt; TypeError: &#39;NoneType&#39; object is not callable<br>
<br>
<br>
</div></div>Maybe you passed None as a callback somewhere?  What is the value of on_connection_open for example?<br>
<br>
_______________________________________________<br>
rabbitmq-discuss mailing list<br>
<a href="mailto:rabbitmq-discuss@lists.rabbitmq.com" target="_blank">rabbitmq-discuss@lists.rabbitmq.com</a><br>
<a href="https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss" target="_blank">https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss</a><br>
</blockquote></div><br></div>
</div></div><br>_______________________________________________<br>
rabbitmq-discuss mailing list<br>
<a href="mailto:rabbitmq-discuss@lists.rabbitmq.com">rabbitmq-discuss@lists.rabbitmq.com</a><br>
<a href="https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss" target="_blank">https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss</a><br>
<br></blockquote></div><br></div>