# Threading and the Elasticsearch Context in Python

**URL:** <https://discuss.elastic.co/t/threading-and-the-elasticsearch-context-in-python/316561>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [October 13, 2022, 4:27pm UTC](https://discuss.elastic.co/t/threading-and-the-elasticsearch-context-in-python/316561 "2022-10-13T16:27:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jhop](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@jhop](https://discuss.elastic.co/u/jhop)\
**Post date:** [October 13, 2022, 4:27pm UTC](https://discuss.elastic.co/t/threading-and-the-elasticsearch-context-in-python/316561/1 "2022-10-13T16:27:56Z")

</div>

When I am programming to connect and use Elasticsearch in Python, is the Context thread safe? i.e. do I need one instance per thread or can I use a static Context for all threads?

---

<div class="post-metadata">

**Author:** ![jhop](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@jhop](https://discuss.elastic.co/u/jhop)\
**Post date:** [October 13, 2022, 6:29pm UTC](https://discuss.elastic.co/t/threading-and-the-elasticsearch-context-in-python/316561/2 "2022-10-13T18:29:11Z")

</div>

For that matter is the elasticsearch client thread safe? Can I have one instance for all my threads?

---

<div class="post-metadata">

**Author:** ![scott\_stash](https://avatars.discourse-cdn.com/v4/letter/s/c89c15/32.png) [@scott\_stash](https://discuss.elastic.co/u/scott_stash)\
**Post date:** [October 13, 2022, 6:57pm UTC](https://discuss.elastic.co/t/threading-and-the-elasticsearch-context-in-python/316561/3 "2022-10-13T18:57:14Z")

</div>

> **[Python Elasticsearch Client — Elasticsearch 7.16.0 documentation](https://elasticsearch-py.readthedocs.io/en/7.x/)**

### Thread safety

The client is thread safe and can be used in a multi threaded environment. Best practice is to create a single global instance of the client and use it throughout your application. If your application is long-running consider turning on [Sniffing](https://elasticsearch-py.readthedocs.io/en/7.x/#sniffing) to make sure the client is up to date on the cluster location.

By default we allow `urllib3` to open up to 10 connections to each node, if your application calls for more parallelism, use the `maxsize` parameter to raise the limit:

---

<div class="post-metadata">

**Author:** ![jhop](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@jhop](https://discuss.elastic.co/u/jhop)\
**Post date:** [October 13, 2022, 6:59pm UTC](https://discuss.elastic.co/t/threading-and-the-elasticsearch-context-in-python/316561/4 "2022-10-13T18:59:58Z")

</div>

Thank you

---

<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:** [November 10, 2022, 7:00pm UTC](https://discuss.elastic.co/t/threading-and-the-elasticsearch-context-in-python/316561/5 "2022-11-10T19:00:36Z")

</div>

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