Recommended number of threads? (in CPython)

Neil Hodgson nyamatongwe+thunder at gmail.com
Thu Oct 29 17:48:53 EDT 2009


mk:

> I found that when using more than several hundred threads causes weird
> exceptions to be thrown *sometimes* (rarely actually, but it happens
> from time to time). 

   If you are running on a 32-bit environment, it is common to run out
of address space with many threads. Each thread allocates a stack and
this allocation may be as large as 10 Megabytes on Linux. With a 4
Gigabyte 32-bit address space this means that the maximum number of
threads will be 400. In practice, the operating system will further
subdivide the address space so only 200 to 300 threads will be possible.
On Windows, I think the normal stack allocation is 1 Megabyte.

   The allocation is only of address space, not memory since memory can
be mapped into this space when it is needed and many threads do not need
very much stack.

   Neil



More information about the Python-list mailing list