Thread-ID - how much could be?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Sep 12 03:29:56 CEST 2014


Ervin Hegedüs wrote:

[...]
>> > My question is: how much thread ID could be totally? Is there any
>> > maximum number? And if the thread reached that, what will be
>> > done? Overlflowed? Couting from 0 again?
[...]
>> There is no upper limit to the thread name other than that you will
>> eventually run out of memory ;)
> 
> thanks - I hope that the memory will not run out by these
> threads... :)
> 
> Anyway, that means, on my system:
> 
>>>> import sys
>>>> print sys.maxint
> 9223372036854775807
> 
> the couter could be 9223372036854775807?
> 
> And after? :)

Suppose you somehow managed to create 9223372036854775807 threads. If your
computer has 16 GB of RAM available, that means that at most each thread
can use:

py> 16*1024*1024*1024/9223372036854775807
1.862645149230957e-09

bytes. That's not allowing any memory for the rest of your program, the
Python interpreter, other processes, or the operating system. That is less
than a single bit and clearly is impossible. You will run out of memory
*long* before reaching 9223372036854775807 threads running at once.

Suppose you don't have all the threads running at once, but you create and
destroy the threads as quickly as possible, so there is never more than one
thread alive at a time (plus the main interpreter thread). Suppose you have
an extremely fast computer that can create and destroy a billion threads
per second, one thread per nanosecond. Then your program would need to run
for:

py> 9223372036854775807*1e-9/60/60/24/365
292.471208677536

years non-stop before reaching a count of sys.maxint. I know that some Linux
systems can have an uptime over a year, perhaps even two years, but I think
that nearly 300 years is asking a bit much. Your hardware probably won't
keep working that long.


-- 
Steven




More information about the Python-list mailing list