Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with pymongo.errors.AutoReconnect: connection closed?

I am writing a python code to update each document in collection. My code is like:

for r, d_50 in enumerate(grid50.find().batch_size(500)):
    self_grid = grid50.find({'_id':d_50['_id']})
    .....
    .....
    (processing process)
    grid50.update({'_id':d_50['_id']},{'$set':{u'big_cell8':{"POI":venue_count, "cell_ids":cell_ids}}})

However, when I run this code, I met problem:

   raise AutoReconnect(str(e))
   pymongo.errors.AutoReconnect: connection closed

Does anyone know how to deal with this problem? Should I add something in my code to deal with this?

like image 296
gladys0313 Avatar asked Apr 21 '15 19:04

gladys0313


1 Answers

From PyMongo Docs -

exception pymongo.errors.AutoReconnect(message='', errors=None)

Raised when a connection to the database is lost and an attempt to auto-reconnect will be made.

In order to auto-reconnect you must handle this exception, recognizing that the operation which caused it has not necessarily succeeded. Future operations will attempt to open a new connection to the database (and will continue to raise this exception until the first successful connection is made).

Basically you'll have to handle this exception so that the application reconnects to mongo and re-runs the function that failed..

like image 156
Abhinav Agrawal Avatar answered Nov 15 '22 15:11

Abhinav Agrawal