# Limit on free-threading threads

**URL:** <https://discuss.python.org/t/limit-on-free-threading-threads/91889>\
**Category:** Python Help\
**Tags:** free-threading\
**Created:** [May 13, 2025, 7:40pm UTC](https://discuss.python.org/t/limit-on-free-threading-threads/91889 "2025-05-13T19:40:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![elis.byberi](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/elis.byberi/32/35937_2.png) [@elis.byberi](https://discuss.python.org/u/elis.byberi)\
**Post date:** [May 13, 2025, 7:40pm UTC](https://discuss.python.org/t/limit-on-free-threading-threads/91889/1 "2025-05-13T19:40:49Z")

</div>

I was reviewing some of these libraries and noticed they’re becoming free-threading friendly. My first thought was: why are they using threads, and is there a way to control or limit how many they use? Do I have to fork each one to manage that? Without limits, they could potentially make the system unresponsive.

> **[Compatibility Status Tracking - py-free-threading](https://py-free-threading.github.io/tracking/)**

---

<div class="post-metadata">

**Author:** ![csm10495](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/csm10495/32/9731_2.png) [@csm10495](https://discuss.python.org/u/csm10495)\
**Post date:** [May 13, 2025, 8:00pm UTC](https://discuss.python.org/t/limit-on-free-threading-threads/91889/2 "2025-05-13T20:00:13Z")

</div>

Technically any library you use could make your system unresponsive. I don’t think free threading support is related to that.

If worried about what a lib might do: you need to look at the source yourself.

---

<div class="post-metadata">

**Author:** ![ngoldbaum](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ngoldbaum/32/12276_2.png) [@ngoldbaum](https://discuss.python.org/u/ngoldbaum)\
**Post date:** [May 13, 2025, 8:09pm UTC](https://discuss.python.org/t/limit-on-free-threading-threads/91889/3 "2025-05-13T20:09:07Z")

</div>

To some extent this is a problem packages already have to deal with. That’s why [threadpoolctl](https://github.com/joblib/threadpoolctl) was a thing long before free-threaded python.

Right now there isn’t anything free-threaded specific and threadpoolctl is more about controlling openblas thread pools than Python-level threading.

IMO as an ecosystem we should probably come up with tools (or figure how to integrate with threadpoolctl) that allow controlling multilevel parallelism. I don’t think we’re quite at a place where people are running into issues caused by several libraries attempting to use internal thread pools leading to oversubscription of system resources, but it’s probably coming soon.

---

<div class="post-metadata">

**Author:** ![elis.byberi](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/elis.byberi/32/35937_2.png) [@elis.byberi](https://discuss.python.org/u/elis.byberi)\
**Post date:** [May 13, 2025, 10:03pm UTC](https://discuss.python.org/t/limit-on-free-threading-threads/91889/4 "2025-05-13T22:03:58Z")

</div>

Yes, but while I can easily contain libraries with poor design choices using containers, I can’t do the same for libraries that rely on free-threading. Doing so would defeat the purpose of free-threading.

That said, users usually don’t need to deal with low-level thread management. For instance, as @ngoldbaum pointed out, libraries like NumPy can be controlled using tools like `threadpoolctl` to limit the number of threads they use internally.

The problem isn’t with `cpu_count` or free-threading on their own, but with the flexibility libraries have to combine them. Even a moderately complex call tree can unintentionally spin up hundreds of threads this way.

---

<div class="post-metadata">

**Author:** ![jamestwebber](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamestwebber/32/12799_2.png) [@jamestwebber](https://discuss.python.org/u/jamestwebber)\
**Post date:** [May 13, 2025, 11:33pm UTC](https://discuss.python.org/t/limit-on-free-threading-threads/91889/5 "2025-05-13T23:33:33Z")

</div>

Does this need a universal solution? It seems like it could be solved by feature requests for specific projects, if/when it becomes an issue.

---

<div class="post-metadata">

**Author:** ![oscarbenjamin](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/oscarbenjamin/32/1209_2.png) [@oscarbenjamin](https://discuss.python.org/u/oscarbenjamin)\
**Post date:** [May 13, 2025, 11:54pm UTC](https://discuss.python.org/t/limit-on-free-threading-threads/91889/6 "2025-05-13T23:54:56Z")

</div>

> [@elis.byberi](#):
>
> why are they using threads, and is there a way to control or limit how many they use?

I think almost all of them are not launching threads. The compatibility is that they are just making changes so they will work if you use the library while you choose to use multiple threads. Any library that does launch its own threads should only do so while providing you with some control over how it does that.

The point @ngoldbaum makes is relevant if you happen to use many libraries that launch their own threads but few libraries do that. Mostly the libraries just become threadsafe and you can choose whether or not to use them with threads.

---

<div class="post-metadata">

**Author:** ![barry-scott](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@barry-scott](https://discuss.python.org/u/barry-scott)\
**Post date:** [May 14, 2025, 9:12am UTC](https://discuss.python.org/t/limit-on-free-threading-threads/91889/7 "2025-05-14T09:12:52Z")

</div>

> [@elis.byberi](#):
>
> The problem isn’t with `cpu_count` or free-threading on their own, but with the flexibility libraries have to combine them

This is not a new problem, how about a C++ program that uses a library that uses threads?

Best practice for a library that uses threads is to have API’s that allow limits on thread use. For example by setting a limit on thread pool sizes.  
Also having a libraries documentation making it clear what use it makes of threads.

But I cannot see how python can policy thread use.

---

<div class="post-metadata">

**Author:** ![elis.byberi](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/elis.byberi/32/35937_2.png) [@elis.byberi](https://discuss.python.org/u/elis.byberi)\
**Post date:** [May 16, 2025, 11:11pm UTC](https://discuss.python.org/t/limit-on-free-threading-threads/91889/8 "2025-05-16T23:11:19Z")

</div>

I’ve been evaluating a platform-agnostic solution, but it quickly becomes overly complex. It requires fine-grained thread filtering, such as distinguishing between I/O-bound and CPU-bound threads, to implement a meaningful and usable thread-limiting mechanism.

While this can work on specific platforms or in targeted use cases, it’s neither suitable nor maintainable within CPython, particularly because it relies on non-orthodox programming techniques and platform-specific assumptions.

I am not going to pursue this in #Ideas
