I am using Threading module in python. How to know how many max threads I can have on my system?
-
3Possible duplicate of [Maximum number of threads per process in Linux?](https://stackoverflow.com/questions/344203/maximum-number-of-threads-per-process-in-linux) – llllllllll Feb 25 '18 at 06:28
-
1This depends on your system and type of work you are conducting in your threads. You divide your CPU time over different threads if they are small tasks you can run more threads than with large tasks. Please also refer to https://stackoverflow.com/questions/481970/how-many-threads-is-too-many – EmbedWise Feb 25 '18 at 06:33
-
@liliscent Not really, the question ask for a way to get the number from the OS with Python. – user202729 Feb 25 '18 at 06:33
-
(although having too many threads isn't good anyway) – user202729 Feb 25 '18 at 06:33
-
1But still knowing max limit can help write optimum code. – Pawandeep Singh Feb 25 '18 at 06:53
-
For mac, if you want to find the hard limit on thread you can generate at a time use `sysctl kern.num_taskthreads` – shivam13juna Jul 24 '21 at 09:18
2 Answers
I am using Threading module in python. How to know how many max threads I can have on my system?
There doesn't seem to be a hard-coded or configurable MAX value that I've ever found, but there is definitely a limit. Run the following program:
import threading
import time
def mythread():
time.sleep(1000)
def main():
threads = 0 #thread counter
y = 1000000 #a MILLION of 'em!
for i in range(y):
try:
x = threading.Thread(target=mythread, daemon=True)
threads += 1 #thread counter
x.start() #start each thread
except RuntimeError: #too many throws a RuntimeError
break
print("{} threads created.\n".format(threads))
if __name__ == "__main__":
main()
I suppose I should mention that this is using Python 3.
The first function, mythread()
, is the function which will be executed as a thread. All it does is sleep for 1000 seconds then terminate.
The main()
function is a for-loop which tries to start one million threads. The daemon
property is set to True simply so that we don't have to clean up all the threads manually.
If a thread cannot be created Python throws a RuntimeError
. We catch that to break
out of the for-loop and display the number of threads which were successfully created.
Because daemon
is set True, all threads terminate when the program ends.
If you run it a few times in a row you're likely to see that a different number of threads will be created each time. On the machine from which I'm posting this reply, I had a minimum 18,835 during one run, and a maximum of 18,863 during another run. And the more you fiddle with the code, as in, the more code you add to this in order to experiment or find more information, you'll find the fewer threads can/will be created.
So, how to apply this to real world.
Well, a server may need the ability to start a triple-digit number of threads, but in most other cases you should re-evaluate your game plan if you think you're going to be generating a large number of threads.
One thing you need to consider if you're using Python: if you're using a standard distribution of Python, your system will only execute one Python thread at a time, including the main thread of your program, so adding more threads to your program or more cores to your system doesn't really get you anything when using the threading module in Python. You can research all of the pedantic details and ultracrepidarian opinions regarding the GIL / Global Interpreter Lock for more info on that.
What that means is that cpu-bound (computationally-intensive) code doesn't benefit greatly from factoring it into threads.
I/O-bound (waiting for file read/write, network read, or user I/O) code, however, benefits greatly from multithreading! So, start a thread for each network connection to your Python-based server.
Threads can also be great for triggering/throwing/raising signals at set periods, or simply to block out the processing sections of your code more logically.

- 851
- 8
- 15
-
1So, are you saying Python's multiprocessing would be better than threading for that first use-case? https://docs.python.org/3/library/multiprocessing.html – Brōtsyorfuzthrāx Oct 10 '22 at 22:55
-
2Good point, and in retrospect I should have mentioned that. Yes, the multiprocessing module would be what you would choose for computer-intensive tasks. – Ian Moote Oct 10 '22 at 23:22
I could see at Max 4096 threads are creating when I run the following code:
import threading
import time
class NumberPrinter(threading.Thread):
def __init__(self, args):
self.counter = args[0]
def run(self) -> None:
time.sleep(5)
print(f"Thread Name: {threading.current_thread().name}, Counter: {self.counter}")
if __name__ == '__main__':
for i in range(10000000):
number_printer = NumberPrinter(args=(i+1,))
number_printer.start()

- 132
- 4