3

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?

1
  • 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, 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. Commented Nov 26, 2021 at 19:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.