Python Glossary Русский

all Python 3threadingfunction

threading.stack_size()

threads

Get or set the stack size for threads created after the call; 0 means the platform default.

Syntax

threading.stack_size([size])

It only affects threads created later and returns the previous value — handy to save and restore. The documented minimum is 32 KiB, but on many systems the real one is higher: you then get ValueError with a different number in the message; use multiples of 4096. The stack is enlarged when deep recursion in a thread (with a raised sys.setrecursionlimit) crashes the whole process instead of raising RecursionError.

Examples

import threading
old = threading.stack_size(4 * 1024 * 1024)  # 4 МиБ для новых потоков
print(old)  # → ? 0 — 0 значит «по умолчанию»
depth = []
def deep(n):
    return 0 if n == 0 else 1 + deep(n - 1)
t = threading.Thread(target=lambda: depth.append(deep(500)))
t.start()
t.join()
threading.stack_size(old)
print(depth)  # → [500]
import threading
try:
    threading.stack_size(1024)
except ValueError as e:
    print(e)  # → ? size must be at least 32768 bytes — минимум зависит от ОС

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy