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?llllllllll– llllllllll2018-02-25 06:28:12 +00:00Commented Feb 25, 2018 at 6: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 stackoverflow.com/questions/481970/how-many-threads-is-too-manyEmbedWise– EmbedWise2018-02-25 06:33:24 +00:00Commented Feb 25, 2018 at 6:33
-
@liliscent Not really, the question ask for a way to get the number from the OS with Python.user202729– user2027292018-02-25 06:33:35 +00:00Commented Feb 25, 2018 at 6:33
-
(although having too many threads isn't good anyway)user202729– user2027292018-02-25 06:33:52 +00:00Commented Feb 25, 2018 at 6:33
-
2But still knowing max limit can help write optimum code.Pawandeep Singh– Pawandeep Singh2018-02-25 06:53:29 +00:00Commented Feb 25, 2018 at 6:53
4 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.
2 Comments
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()
1 Comment
There's no actual "thread" in python as is for python with GIL. Simply speaking, what the threading module does is create contexts for "threads" to switch around (not really but similar thing).
After adding traceback.print_exc() to code in https://stackoverflow.com/a/64406494/16065744 under except, it prints
Exception ignored in thread started by: <bound method Thread._bootstrap of <Thread(Thread-542309 (mythread), initial daemon)>>
MemoryError:
So, the maximum limit on number of threads in python depends on how much memory the system have.
NOTE:
Implementation of threading can be different between platforms, which leads to different behavior of former code.
Env Info:
- Window 11
- python 3.11