title: Is there a way to exit/end old recursions (function()) without finishing them?
body: <p>I created a very short program that forces the <code>RecursionError: maximum recursion depth exceeded while pickling an object</code> to occur:</p>
  <pre><code>import sys
  sys.setrecursionlimit(1)
  
  AB,CD=0,0
  
  def A():
      global AB
      AB+=1
      print('A',AB,CD)
      # End all () except for A
      B()
      print('A2')
  
  def B():
      global AB
      AB+=1
      print('B')
      C()
      print('B2')
  
  def C():
      global CD
      CD+=1
      print('C')
      D()
      A()
      print('D2')
  
  def D():
      global CD
      CD+=1
      print('D')
  
  A()
  </code></pre>
  <p>What you will get in shell is:</p>
  <pre><code>A 1 0
  B
  C
  #&lt;...&gt; (a couple of lines that I cut out to make the post clearer)
  C
  D
  A 9 8
  Traceback (most recent call last):
    File &quot;D:\!Z pulpitu\Python\VTX RE.py&quot;, line 34, in &lt;module&gt;
      A()
    File &quot;D:\!Z pulpitu\Python\VTX RE.py&quot;, line 11, in A
      B()
    File &quot;D:\!Z pulpitu\Python\VTX RE.py&quot;, line 18, in B
      C()
  #&lt;...&gt; (a couple of lines that I cut out to make the post clearer, screenshot below)
    File &quot;D:\!Z pulpitu\Python\VTX RE.py&quot;, line 11, in A
      B()
    File &quot;D:\!Z pulpitu\Python\VTX RE.py&quot;, line 17, in B
      print('B')
  RecursionError: maximum recursion depth exceeded while pickling an object
  </code></pre>
  <p>The question is: Can I somehow end/exit/quit/close/forget all the iterations of <code>B()</code>, <code>C()</code> and <code>D()</code> when I am in <code>A()</code> (more specifically where &quot;# End all () except for A&quot; is located) without resetting the values <code>AB</code> and <code>CD</code>? And will it solve the <code>RecursionError</code>?</p>
  <p>Cause as far as I am aware, while <code>D()</code> ends itself cause it doesn't call any other recursion, <code>B()</code> and <code>C()</code> are still running in the background waiting for their called functions to finish so they can get to <code>print('B2')</code> and <code>print('C2')</code> respectively, taking unnecessary memory (which, with higher recursionlimit and more complicated objects can cause a <code>MemoryError</code>)</p>