2

I need to increase the recursion limit/stack size for a Python program (just a CP problem, not production code). I've found several ways to do this (e.g. this and this):

import resource, sys
resource.setrlimit(resource.RLIMIT_STACK, (2**29,-1))
sys.setrecursionlimit(10**6)

or

import sys
import threading

threading.stack_size(67108864) # 64MB stack
sys.setrecursionlimit(2 ** 20)
thread = threading.Thread(target=main)
thread.start()

What's the difference between the two methods and which one should I prefer?

planetp
  • 14,248
  • 20
  • 86
  • 160
  • Don't do either -- both are dangerous. Why do you need to set the stack size or recursion limit? When you need to mess with the stack size, it's almost always an [xy problem](https://meta.stackexchange.com/a/233676/399876), regardless of whether it's production code or not. The fact that pretty much every other language runtime doesn't offer this feature should indicate that it's not something to be messed with. Depeding on the problem, you can often refactor whatever code you're running to be iterative, for example. – ggorlen Nov 26 '21 at 19:02

0 Answers0