Search code examples
mongoenginepython-rq

How to reuse Mongoengine connection in python rq?


I need to sync a lot of data on the back-ground using mongoengine through python rq and i would like to reuse connection. Syncing will be done all the time and i don't wanna open connection to mongo every time syncing process starts


Solution

  • MongoEngine internally keeps the Pymongo.MongoClient in a global dict in mongoengine.connection. You aren't supposed to interact with it, but if you need to, it is accessible with:

    from mongoengine import connect
    from mongoengine.connection import _connections
    
    connect()
    
    print(_connections) # {'default': MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True, read_preference=Primary())}
    

    If you just want to use the MongoClient and issue pymongo queries, that's perfect but if you intend to use multiprocessing, make sure you read this, as the MongoClient is not fork-safe