title: How can I write a Python decorator to increase stackdepth?
body: <h2>BACKGROUND</h2>
  
  <p>When playing around, I often write simple recursive functions looking something like:</p>
  
  <pre><code>def f(a,b):
      if a&gt;=0 and b&gt;=0:
          return min( f(a-1,b) , f(b,a-1) ) # + some cost that depends on a,b
      else:
          return 0
  </code></pre>
  
  <p>(For example, when computing weighted edit distances, or evaluating recursively defined mathematical formulas.)</p>
  
  <p>I then use a memoizing decorator to cache the results automatically.</p>
  
  <h2>PROBLEM</h2>
  
  <p>When I try something like f(200,10) I get:</p>
  
  <pre><code>RuntimeError: maximum recursion depth exceeded
  </code></pre>
  
  <p>This is as expected because the recursive implementation exhausts Python's stack space/ recursion limits.</p>
  
  <h2>WORKAROUNDS</h2>
  
  <p>I usually work around this problem by one of:</p>
  
  <ul>
  <li>Increasing recursion limit with sys.setrecursionlimit (only works up to about 1000 depth)</li>
  <li>Using a for loop to fill up the cache for smaller values</li>
  <li>Changing the function to use a list as a manual stack (via append and pop calls) (in other words, moving from a recursive implementation to an iterative one)</li>
  <li>Using an alternative programming language</li>
  </ul>
  
  <p>but I find all of these quite error prone.</p>
  
  <h2>QUESTION</h2>
  
  <p>Is there a way to write an @Bigstack decorator that would simulate the effect of having a really big stack?</p>
  
  <p>Note that my functions normally make several recursive function calls so this is not the same as tail recursion - I really do want to save all the internal state of each function on the stack.</p>
  
  <h2>WHAT I'VE TRIED</h2>
  
  <p>I've been thinking about using a list of generator expressions as my stack.  By probing the stackframe I could work out when the function has been called recursively and then trigger an exception to return to the decorator code.  However, I can't work out a way of gluing these ideas together to make anything that actually works.</p>
  
  <p>Alternatively, I could try accessing the abstract syntax tree for the function and try transforming calls to recursive functions to yield statements, but this seems like it's heading in the wrong direction.</p>
  
  <p>Any suggestions?</p>
  
  <p>EDIT</p>
  
  <p>It certainly looks like I am misusing Python, but another approach I have been considering is to use a different thread for each block of, say, 500 stack frames and then insert queues between each consecutive pair of threads - one queue for arguments, and another queue for return values.  (Each queue will have at most one entry in it.)  I think this probably doesn't work for some reason - but I'll probably only work out why after I've tried to implement it.</p>