maximum number of threads

Jean-Paul Calderone exarkun at divmod.com
Wed Jan 10 15:19:22 CET 2007


On Wed, 10 Jan 2007 12:11:59 -0200, Felipe Almeida Lessa <felipe.lessa at gmail.com> wrote:
>On 1/10/07, Laurent Pointal <laurent.pointal at limsi.fr> wrote:
>>This is a system configurable limit (up to a maximum).
>>
>>See ulimit man pages.
>>
>>test
>>
>>         ulimit -a
>>
>>to see what are the current limits, and try with
>>
>>         ulimit -u 2000
>>
>>to modify the maximum number of user process (AFAIK each thread use a
>>process entry on Linux)
>
>I don't think it's only this.

Indeed you are correct.  The actual limit you are hitting is the size
of your address space.  Each thread is allocated 8MB of stack.  382
threads consumes about 3GB of address space.  Even though most of this
memory isn't actually allocated, the address space is still used up.  So,
when you try to create the 383rd thread, the kernel can't find anyplace
to put its stack.  So you can't create it.

Try reducing your stack size or reducing the number of threads you create.
There's really actually almost no good reason to have this many threads,
even though it's possible.

    exarkun at charm:~$ python Desktop/test.py
    50
    100
    150
    200
    250
    300
    350
    Exception raised: can't start new thread
    
    Biggest number of threads: 382
    exarkun at charm:~$ ulimit -Ss 4096
    exarkun at charm:~$ python Desktop/test.py
    50
    100
    150
    200
    250
    300
    350
    400
    450
    500
    550
    600
    650
    700
    750
    Exception raised: can't start new thread
    
    Biggest number of threads: 764
    exarkun at charm:~$
    
Jean-Paul



More information about the Python-list mailing list