question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Connection drops when handling tasks with a long duration

See original GitHub issue

I’m starting to get the same error as #418 even when I adjust the heartbeat connection parameter. My tasks take usually around 5 mins, but even then, when I set the heartbeat to both 0 and some very large number (ie: 60000)

My code Is essentially identical to the issue I mentioned above and the error I receive is this: Traceback (most recent call last): File "instagram_worker.py", line 43, in <module> channel.start_consuming() File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 1780, in start_consuming self.connection.process_data_events(time_limit=None) File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 716, in process_data_events self._dispatch_channel_events() File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 518, in _dispatch_channel_events impl_channel._get_cookie()._dispatch_events() File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 1403, in _dispatch_events evt.body) File "instagram_worker.py", line 37, in dataHandler channel.basic_ack(delivery_tag=method.delivery_tag) File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 1988, in basic_ack self._flush_output() File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 1250, in _flush_output *waiters) File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 474, in _flush_output result.reason_text) pika.exceptions.ConnectionClosed: (-1, "error(104, 'Connection reset by peer')")

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

20reactions
gmrcommented, Jan 25, 2018

Try something like this:

import re
import json
import threading

from google.cloud import bigquery
import pandas as pd
import pika
from unidecode import unidecode


def process_export(url, tablename):
    df = pd.read_csv(csvURL, encoding="utf-8")
    print("read in the csv")
    columns = list(df)
    ascii_only_name = [unidecode(name) for name in columns]
    cleaned_column_names = [re.sub("[^a-zA-Z0-9_ ]", "", name) for name in ascii_only_name]
    underscored_names = [name.replace(" ", "_") for name in cleaned_column_names]
    valid_gbq_tablename = "test." + tablename
    df.columns = underscored_names

    # try:
    df.to_gbq(valid_gbq_tablename, "some_project", if_exists="append", verbose=True, chunksize=10000)
    # print("Finished Exporting")
    # except Exception as error:
    #     print("unable to export due to: ")
    #     print(error)
    #     print()


def data_handler(channel, method, properties, body):
    body = json.loads(body)

    thread = threading.Thread(target=process_export, args=(body["csvURL"], body["tablename"]))
    thread.start()
    while thread.is_alive():  # Loop while the thread is processing
        channel._connection.sleep(1.0)
    print('Back from thread')
    channel.basic_ack(delivery_tag=method.delivery_tag)


def main():
    params = pika.ConnectionParameters(host='localhost', heartbeat=60)
    connection = pika.BlockingConnection(params)
    channel = connection.channel()
    channel.queue_declare(queue="some_queue", durable=True)
    channel.basic_qos(prefetch_count=1)
    channel.basic_consume(data_handler, queue="some_queue")
    try:
        channel.start_consuming()
    except KeyboardInterrupt:
        channel.stop_consuming()
    channel.close()


if __name__ == '__main__':
    main()
1reaction
lukebakkencommented, Jan 25, 2018

Thanks @gmr

Read more comments on GitHub >

github_iconTop Results From Across the Web

Internet Keeps Dropping? Here's why, and what you need ...
Internet keeps dropping? Here are some quick and easy fixes that you can apply if your internet broadband keeps disconnecting.
Read more >
What Causes High Speed Internet Connections to ...
Another common cause of disconnection is an overloaded router. The router is the device that stands between your network and the Internet, accepting...
Read more >
5 Reasons Why Your Internet Keeps Disconnecting—and ...
Unpredictable internet disconnections and Wi-Fi drops can be irritating and mysterious. Because these issues tend to come and go randomly, ...
Read more >
How to Fix Wi-Fi Connection Drops
There often seems to be no reason behind Wi-Fi connections that randomly drop off or weaken. However, there are actually several common ...
Read more >
Internet Keeps Dropping? Here's a Guaranteed Fix!
Want the guaranteed solution to your Internet keeps dropping issues? Get Speedify, the only VPN for using multiple connections at the same time!...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found