[python-win32] 1MB Thread Stack Size

Andrew MacIntyre Andrew.MacIntyre at aba.gov.au
Thu Jun 2 03:46:15 CEST 2005


> I have a program that needs to create a great deal of 
> threads.  Unfortunately, I cannot seem to find a way to lower 
> the 1MB default stack size per thread.  The threading module 
> does not seem to support setting the stack size explicitly.  
> I have 1GB of memory on my system so I can only create about 
> 1000 threads before I receive the following error trace:
> 
>   File "C:\Python24\lib\threading.py", line 442, in __bootstrap 
>     self.run()
>   File "C:\Python24\lib\threading.py", line 422, in run 
>     self.__target(*self.__args, **self.__kwargs)
>   File "C:\tagent\globalModule.py", line 190, in shell 
>     self.target()
>   File "C:\tagent\agent.py", line 132, in runLoop 
>     self.timeSliceEvent(currentTime,delta)
>   File "C:\tagent\agent.py", line 397, in timeSliceEvent 
>     actorInstance.threadStart()
>   File "C:\tagent\agent.py", line 117, in threadStart 
>     threading.Thread.start(self)
>   File "C:\Python24\lib\threading.py", line 416, in start 
>     _start_new_thread(self.__bootstrap, ())
>   error: can't start new thread 
> 
> Any ideas on how to lower the stack size? 

Python's thread module is hard-coded to use Windows' default thread 
stack size, which according to the docs I have will be the same as the 
process' primary thread stack size (which is usually set at link time).
These same docs also indicate that thread creation fails if all memory 
for the thread stack cannot be committed (some other OSes don't 
attempt to commit all stack space on thread creation).

If you can find a way to modify the stack-size setting of python.exe 
this might work for you, but be warned that this could also cause other 
stack related failures as the primary thread usually requires more 
stack than other threads.

If you're in a position to recompile Python, it doesn't take much to 
change the thread module's hard-coded thread stack size.

In my spare time I've been working on a patch to allow the default 
thread stack size to be changed programmatically - its mostly complete 
except for doc updates, and I hope to get it into Python 2.5.

----------------------------------------------------------------------
Andrew MacIntyre                  \ email: andrew.macintyre at aba.gov.au
Planning Branch                    \ tel:  +61 2 6256 2812
Australian Broadcasting Authority   \ fax: +61 2 6253 3277
-> "These thoughts are mine alone!" <--------------------------------- 
> 
> Chad 
> 
> 


More information about the Python-win32 mailing list