Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximum limit on number of threads in python

I am using Threading module in python. How to know how many max threads I can have on my system?

like image 386
Pawandeep Singh Avatar asked Feb 25 '18 06:02

Pawandeep Singh


People also ask

What is the maximum number of threads in Python?

Generally, Python only uses one thread to execute the set of written statements. This means that in python only one thread will be executed at a time.

How many threads can run at once?

A single CPU core can have up-to 2 threads per core. For example, if a CPU is dual core (i.e., 2 cores) it will have 4 threads.

Can Python threads run on multiple cores?

Threading Library. Above we alluded to the fact that Python on the CPython interpreter does not support true multi-core execution via multithreading. However, Python DOES have a Threading library.

How do you determine the number of threads in Python?

The optimal number of threads is usually chosen based on factors such as other applications and services running on the same machine. We will chose 8 worker threads because many personal computers have 8 CPU cores and one worker thread per core seems like a good number for how many threads to run at once.


1 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.

like image 119
Ian Moote Avatar answered Oct 02 '22 16:10

Ian Moote