how do I turn off the timeout?

1,903 views
Skip to first unread message

simon mackenzie

unread,
Jun 28, 2019, 3:53:30 PM6/28/19
to rabbitmq-users
I am trying to send log messages. This works fine and I can see the messages on the consumer console. However the producer automatically disconnects after about 180 seconds. So when I am working in a jupyter notebook I just stop for 3 minutes and the logs no longer work. Presumably there is a timeout setting somewhere but where?

This is example producer code. 

import pika
from time import sleep
connection = pika.BlockingConnection()
channel = connection.channel()
channel.queue_declare(queue='log_queue')
channel.basic_publish(exchange='', routing_key='log_queue', body='Hello')
sleep(180)
channel.basic_publish(exchange='', routing_key='log_queue', body='Hello')

First message received fine. Second message 
StreamLostError: Stream connection lost: ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None) 

Luke Bakken

unread,
Jun 28, 2019, 6:52:32 PM6/28/19
to rabbitmq-users
Hello,

When you sleep, you block Pika's I/O loop and thus you block heartbeats. RabbitMQ expects heartbeats every 60 seconds. By sleeping 180 seconds, RabbitMQ thinks the connection is dead and closes it.

So, don't block the I/O loop.

Thanks,
Luke

simon mackenzie

unread,
Jun 28, 2019, 6:58:15 PM6/28/19
to rabbitm...@googlegroups.com
I only put the sleep in to illustrate. I am working in a Jupiter notebook. Sometimes I have a cup of tea for 3 minutes then want to run another function that logs a message.

How do I have an app that sends a message infrequently without the queue closing? Or does rabbitmq not work for infrequent messages?

--
You received this message because you are subscribed to the Google Groups "rabbitmq-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-user...@googlegroups.com.
To post to this group, send email to rabbitm...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/rabbitmq-users/af0243ef-d18d-4180-ae96-dde9ca069515%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Luke Bakken

unread,
Jun 28, 2019, 7:06:05 PM6/28/19
to rabbitmq-users
Hi Simon,

Please note that "Jupiter notebook" doesn't necessarily mean anything to people reading this list.

When you instantiate a BlockingConnection object, it sets up an I/O loop in the background on the same thread that owns the object. If that thread is ever blocked, it will prevent heartbeats from being sent to and received from RabbitMQ. This is not unique to Pika, nor is it really unique to RabbitMQ but is a function of how event loops work in conjunction with threading.

Since I don't have a complete code sample to work with, I can only guess what you are trying to accomplish. Using Pika, if you want to send a message every 180 seconds, you would do it this way:

import pika

connection = pika.BlockingConnection()
channel = connection.channel()
channel.queue_declare(queue='log_queue')
while True:
    channel.basic_publish(exchange='', routing_key='log_queue', body='Hello')
    connection.process_data_events(180)

Another option is to run all of your Pika code on a dedicated thread. You can also disable heartbeats but that is generally a bad decision as dead connections may not be detected quickly.

If you can provide more code, or a complete code sample, I could review it and give suggestions.

Thanks -
Luke

simon mackenzie

unread,
Jun 28, 2019, 7:07:27 PM6/28/19
to rabbitm...@googlegroups.com
Also this is a blocking connection.so there is no loop. I have found if I  set heartbeat to zero then it works. Perhaps there is better way using a select connection?

On Fri, 28 Jun 2019, 23:52 Luke Bakken, <lba...@pivotal.io> wrote:
--

Luke Bakken

unread,
Jun 28, 2019, 7:11:20 PM6/28/19
to rabbitmq-users
Hi Simon,

Please see the simple example I provided in another response. Yes, BlockingConnection sets up an I/O loop.

Providing more code or an explanation of how you're using Pika with the "Jupiter notebook" product and we can give suggestions.

Using a SelectConnection is definitely an option but the same caveats apply as BlockingConnection uses SelectConnection anyway.

Thanks -
Luke


On Friday, June 28, 2019 at 4:07:27 PM UTC-7, simon mackenzie wrote:
Also this is a blocking connection.so there is no loop. I have found if I  set heartbeat to zero then it works. Perhaps there is better way using a select connection?

On Fri, 28 Jun 2019, 23:52 Luke Bakken, <lba...@pivotal.io> wrote:
Hello,

When you sleep, you block Pika's I/O loop and thus you block heartbeats. RabbitMQ expects heartbeats every 60 seconds. By sleeping 180 seconds, RabbitMQ thinks the connection is dead and closes it.

So, don't block the I/O loop.

Thanks,
Luke

On Friday, June 28, 2019 at 12:53:30 PM UTC-7, simon mackenzie wrote:
I am trying to send log messages. This works fine and I can see the messages on the consumer console. However the producer automatically disconnects after about 180 seconds. So when I am working in a jupyter notebook I just stop for 3 minutes and the logs no longer work. Presumably there is a timeout setting somewhere but where?

This is example producer code. 

import pika
from time import sleep
connection = pika.BlockingConnection()
channel = connection.channel()
channel.queue_declare(queue='log_queue')
channel.basic_publish(exchange='', routing_key='log_queue', body='Hello')
sleep(180)
channel.basic_publish(exchange='', routing_key='log_queue', body='Hello')

First message received fine. Second message 
StreamLostError: Stream connection lost: ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None) 

--
You received this message because you are subscribed to the Google Groups "rabbitmq-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-users+unsubscribe@googlegroups.com.
To post to this group, send email to rabbitmq-users@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages