# What does persistent connection in the Elasticsearch Python API mean?

**URL:** <https://discuss.elastic.co/t/what-does-persistent-connection-in-the-elasticsearch-python-api-mean/22950>\
**Category:** Elasticsearch\
**Created:** [March 27, 2015, 7:09pm UTC](https://discuss.elastic.co/t/what-does-persistent-connection-in-the-elasticsearch-python-api-mean/22950 "2015-03-27T19:09:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yogesh\_Kansal](https://avatars.discourse-cdn.com/v4/letter/y/57b2e6/32.png) [@Yogesh\_Kansal](https://discuss.elastic.co/u/Yogesh_Kansal)\
**Post date:** [March 27, 2015, 7:09pm UTC](https://discuss.elastic.co/t/what-does-persistent-connection-in-the-elasticsearch-python-api-mean/22950/1 "2015-03-27T19:09:29Z")

</div>

Hi,

I am using the Python Elasticsearch API  
[http://www.elasticsearch.org/guide/en/elasticsearch/client/python-api/current/](http://www.elasticsearch.org/guide/en/elasticsearch/client/python-api/current/)  
to interact with ES in my Python Flask application (server is Python  
Tornado). Currently, as soon as the app gets a new user request, it does  
esclient = Elasticsearch("127.0.0.1") and then uses this esclient to search  
for the required data. But I recently read that ES has persistent  
connections. So,

1. Should I save the esclient somewhere and reuse it instead of doing  
esclient = Elasticsearch("127.0.0.1") for every request?
2. Would there be some resource leak if I forget about esclient after  
the request and open a new one every time? Is there a way to close this  
open connection?

I am also doing the same for another db, Redis in my app. Sorry, might seem  
like a basic question but I am a bit confused.

Thanks-in-advance!

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/19d47985-087f-41c1-b524-1f6ac26fd63c%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/19d47985-087f-41c1-b524-1f6ac26fd63c%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![honzakral](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/honzakral/32/44958_2.png) [@honzakral](https://discuss.elastic.co/u/honzakral)\
**Post date:** [March 27, 2015, 9:42pm UTC](https://discuss.elastic.co/t/what-does-persistent-connection-in-the-elasticsearch-python-api-mean/22950/2 "2015-03-27T21:42:41Z")

</div>

Hi,

For both elasticsearch and redis python clients you should just have one  
instance and keep reusing it.

If you create new ones there will be some connections left hanging (though  
gc should pick them up) but also both your app and elasticsearch itself  
will be paying tthe overhead of creating and maintaining additional  
connections, which is not good.

Hope this helps,  
Honza

On Fri, Mar 27, 2015 at 8:09 PM, Yogesh [bindasyogesh@gmail.com](mailto:bindasyogesh@gmail.com) wrote:

> Hi,
> 
> I am using the Python Elasticsearch API  
> [http://www.elasticsearch.org/guide/en/elasticsearch/client/python-api/current/](http://www.elasticsearch.org/guide/en/elasticsearch/client/python-api/current/)  
> to interact with ES in my Python Flask application (server is Python  
> Tornado). Currently, as soon as the app gets a new user request, it does  
> esclient = Elasticsearch("127.0.0.1") and then uses this esclient to search  
> for the required data. But I recently read that ES has persistent  
> connections. So,
> 
> 1. Should I save the esclient somewhere and reuse it instead of doing  
> esclient = Elasticsearch("127.0.0.1") for every request?
> 2. Would there be some resource leak if I forget about esclient after  
> the request and open a new one every time? Is there a way to close this  
> open connection?
> 
> I am also doing the same for another db, Redis in my app. Sorry, might  
> seem like a basic question but I am a bit confused.
> 
> Thanks-in-advance!
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/19d47985-087f-41c1-b524-1f6ac26fd63c%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/19d47985-087f-41c1-b524-1f6ac26fd63c%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/19d47985-087f-41c1-b524-1f6ac26fd63c%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/19d47985-087f-41c1-b524-1f6ac26fd63c%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
Honza Král  
Python Engineer  
[honza.kral@elastic.co](mailto:honza.kral@elastic.co)

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAC4VrtwsJoVmw1C-2wixjOcvu2722JRcsqtAdC6ZpgwXs3Vjqg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAC4VrtwsJoVmw1C-2wixjOcvu2722JRcsqtAdC6ZpgwXs3Vjqg%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Yogesh\_Kansal](https://avatars.discourse-cdn.com/v4/letter/y/57b2e6/32.png) [@Yogesh\_Kansal](https://discuss.elastic.co/u/Yogesh_Kansal)\
**Post date:** [March 28, 2015, 1:52pm UTC](https://discuss.elastic.co/t/what-does-persistent-connection-in-the-elasticsearch-python-api-mean/22950/3 "2015-03-28T13:52:24Z")

</div>

Thanks Honza. I'll do that.

On Sat, Mar 28, 2015 at 3:12 AM, Honza Král [honza.kral@elastic.co](mailto:honza.kral@elastic.co) wrote:

> Hi,
> 
> For both elasticsearch and redis python clients you should just have one  
> instance and keep reusing it.
> 
> If you create new ones there will be some connections left hanging (though  
> gc should pick them up) but also both your app and elasticsearch itself  
> will be paying tthe overhead of creating and maintaining additional  
> connections, which is not good.
> 
> Hope this helps,  
> Honza
> 
> On Fri, Mar 27, 2015 at 8:09 PM, Yogesh [bindasyogesh@gmail.com](mailto:bindasyogesh@gmail.com) wrote:
> 
> > Hi,
> > 
> > I am using the Python Elasticsearch API  
> > [http://www.elasticsearch.org/guide/en/elasticsearch/client/python-api/current/](http://www.elasticsearch.org/guide/en/elasticsearch/client/python-api/current/)  
> > to interact with ES in my Python Flask application (server is Python  
> > Tornado). Currently, as soon as the app gets a new user request, it does  
> > esclient = Elasticsearch("127.0.0.1") and then uses this esclient to search  
> > for the required data. But I recently read that ES has persistent  
> > connections. So,
> > 
> > 1. Should I save the esclient somewhere and reuse it instead of doing  
> > esclient = Elasticsearch("127.0.0.1") for every request?
> > 2. Would there be some resource leak if I forget about esclient after  
> > the request and open a new one every time? Is there a way to close this  
> > open connection?
> > 
> > I am also doing the same for another db, Redis in my app. Sorry, might  
> > seem like a basic question but I am a bit confused.
> > 
> > Thanks-in-advance!
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/19d47985-087f-41c1-b524-1f6ac26fd63c%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/19d47985-087f-41c1-b524-1f6ac26fd63c%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/19d47985-087f-41c1-b524-1f6ac26fd63c%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/19d47985-087f-41c1-b524-1f6ac26fd63c%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> 
> --  
> Honza Král  
> Python Engineer  
> [honza.kral@elastic.co](mailto:honza.kral@elastic.co)
> 
> --  
> You received this message because you are subscribed to a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/a8aK7YrfeZg/unsubscribe](https://groups.google.com/d/topic/elasticsearch/a8aK7YrfeZg/unsubscribe).  
> To unsubscribe from this group and all its topics, send an email to  
> [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/CAC4VrtwsJoVmw1C-2wixjOcvu2722JRcsqtAdC6ZpgwXs3Vjqg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAC4VrtwsJoVmw1C-2wixjOcvu2722JRcsqtAdC6ZpgwXs3Vjqg%40mail.gmail.com)  
> [https://groups.google.com/d/msgid/elasticsearch/CAC4VrtwsJoVmw1C-2wixjOcvu2722JRcsqtAdC6ZpgwXs3Vjqg%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CAC4VrtwsJoVmw1C-2wixjOcvu2722JRcsqtAdC6ZpgwXs3Vjqg%40mail.gmail.com?utm_medium=email&utm_source=footer)  
> .
> 
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CADM0w%3DiZbaTsZWoXJ1zhD0Lk7\_48jumZy2KPPoho5bh4QbiRmQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CADM0w%3DiZbaTsZWoXJ1zhD0Lk7_48jumZy2KPPoho5bh4QbiRmQ%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 12:23am UTC](https://discuss.elastic.co/t/what-does-persistent-connection-in-the-elasticsearch-python-api-mean/22950/4 "2017-07-06T00:23:16Z")

</div>


