I am using the Python Elasticsearch API to interact with ES in my application. Currently, as soon as the app gets a user request, it does esclient = Elasticsearch("127.0.0.1")
and then uses this esclient
to search for required data. But I recently read that ES has persistent connections. So,
- Should I save the
esclient
somewhere and reuse it? If yes, how do I do that? - Would there be some resource leak if I forget about
esclient
after the request and open a new one next time? If yes, how do I cure this/ close the open connection?
Also, same questions for the memcached/ Redis python APIs. I do client = Client("127.0.0.1")
multiple times during each user request.
Sorry, might seem like a stupid question but I am a bit confused.
Thanks-in-advance!
For elasticsearch python client you should just have one instance and keep reusing it.
If it's a Django or Flask application, you can create an elasticsearch specific resource file and create a connection as follows and reuse it everywhere.
Import ESCLIENT everywhere and reuse.