title: How can I stop the execution of a Python function from outside of it?
body: <p>So I have this library that I use and within one of my functions I call a function from that library, which happens to take a really long time. Now, at the same time I have another thread running where I check for different conditions, what I want is that if a condition is met, I want to cancel the execution of the library function.</p>
  
  <p>Right now I'm checking the conditions at the start of the function, but if the conditions happen to change while the library function is running, I don't need its results, and want to return from it.</p>
  
  <p>Basically this is what I have now.</p>
  
  <pre><code>def my_function():
      if condition_checker.condition_met():
          return
      library.long_running_function()
  </code></pre>
  
  <p>Is there a way to run the condition check every second or so and return from my_function when the condition is met?</p>
  
  <p>I've thought about decorators, coroutines, I'm using 2.7 but if this can only be done in 3.x I'd consider switching, it's just that I can't figure out how.</p>