# Elasticsearch multiple connections using python

**URL:** <https://discuss.elastic.co/t/elasticsearch-multiple-connections-using-python/290458>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [November 29, 2021, 4:14pm UTC](https://discuss.elastic.co/t/elasticsearch-multiple-connections-using-python/290458 "2021-11-29T16:14:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zouhair](https://avatars.discourse-cdn.com/v4/letter/z/a587f6/32.png) [@Zouhair](https://discuss.elastic.co/u/Zouhair)\
**Post date:** [November 29, 2021, 4:14pm UTC](https://discuss.elastic.co/t/elasticsearch-multiple-connections-using-python/290458/1 "2021-11-29T16:14:17Z")

</div>

Hi, I am using python to connect to my Elasticsearch and execute a search query.  
I have noticed that the python Elasticsearch module is making 2 connexion to the specified cluster.

is that normal ?  
if not how can i correct this behaviour ?

thank in advance for your help  
here is the code

```auto
try:
        #Connect to the specified Cluster
        print(str(datetime.now()) + " Tentative de connexion au cluster " + CLUSTER)
        es = Elasticsearch([CLUSTER],
            http_auth=(user, password),
            ssl_context=ssl_context
        )
        # Check cluster connection with a ping.
        # if ping Ok then the cluster connection is OK
        try :
            es.ping()
            print(str(datetime.now()) + " Connexion au cluster "+ CLUSTER +" reussi")
            return es
        except exceptions as ex:
            print("Exception : {}".format(datetime.now()))
            print(traceback.format_exc())
            exit()
    except exceptions as e:
        print("Exception : {}".format(datetime.now()))
        print(e)
        exit()

```

---

<div class="post-metadata">

**Author:** ![elasticforme](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@elasticforme](https://discuss.elastic.co/u/elasticforme)\
**Post date:** [November 30, 2021, 1:37am UTC](https://discuss.elastic.co/t/elasticsearch-multiple-connections-using-python/290458/2 "2021-11-30T01:37:59Z")

</div>

where did you see it is creating two connection?

---

<div class="post-metadata">

**Author:** ![Zouhair](https://avatars.discourse-cdn.com/v4/letter/z/a587f6/32.png) [@Zouhair](https://discuss.elastic.co/u/Zouhair)\
**Post date:** [November 30, 2021, 8:50am UTC](https://discuss.elastic.co/t/elasticsearch-multiple-connections-using-python/290458/3 "2021-11-30T08:50:49Z")

</div>

I see it in my log every time a execute the script

```auto
2021-11-29 16:11:20.732000 Tentative de connexion au cluster https://my_cluster
2021-11-29 16:11:20.928000 Connexion au cluster https://my_cluster/ reussi
2021-11-29 16:11:20.946000 Tentative de connexion au cluster https://my_cluster/
2021-11-29 16:11:21.119000 Connexion au cluster https://my_cluster/ reussi

```

sorry the log is in french but it says:  
"2021-11-29 16:11:20.732000 trying to connect to the cluster https://my\_cluster  
2021-11-29 16:11:20.928000 connection to the cluster https://my\_cluster ok  
2021-11-29 16:11:20.946000 trying to connect to the cluster https://my\_cluster  
2021-11-29 16:11:21.119000 connection to the cluster https://my\_cluster ok"

---

<div class="post-metadata">

**Author:** ![elasticforme](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@elasticforme](https://discuss.elastic.co/u/elasticforme)\
**Post date:** [November 30, 2021, 1:52pm UTC](https://discuss.elastic.co/t/elasticsearch-multiple-connections-using-python/290458/4 "2021-11-30T13:52:50Z")

</div>

it does not mean it is keeping that two connection. it might be closing and opening as needed.  
though I am not internal developer of python api. I will not worry to much until it causing a problem.

it seems like first one is you creating pointer to cluster i.e es =  
and second you are trying es.ping ( which might be pingining cluster and closing after that)

---

<div class="post-metadata">

**Author:** ![leandrojmp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leandrojmp/32/107231_2.png) [@leandrojmp](https://discuss.elastic.co/u/leandrojmp)\
**Post date:** [November 30, 2021, 2:06pm UTC](https://discuss.elastic.co/t/elasticsearch-multiple-connections-using-python/290458/5 "2021-11-30T14:06:25Z")

</div>

What is the rest of your python script? Can you share your full script to help understand why you are going into the `try` blocks twice?

---

<div class="post-metadata">

**Author:** ![Zouhair](https://avatars.discourse-cdn.com/v4/letter/z/a587f6/32.png) [@Zouhair](https://discuss.elastic.co/u/Zouhair)\
**Post date:** [November 30, 2021, 3:00pm UTC](https://discuss.elastic.co/t/elasticsearch-multiple-connections-using-python/290458/6 "2021-11-30T15:00:50Z")

</div>

I figured it out !

i Had another method calling the connexion Method, so I fixed it

thanks for your help

---

<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:** [December 28, 2021, 3:00pm UTC](https://discuss.elastic.co/t/elasticsearch-multiple-connections-using-python/290458/7 "2021-12-28T15:00:57Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
