sympy/sympy — pull request #8297 by jcrist (2014-10-24 00:19:56+00:00)
Title: [WIP] Added `timeout_after` context manager

*Note: This is just a proof of concept. See #8295 for another option of implementing. I like this one better, but both attempt to solve the same issue.*

Added contextmanager to make timeouts on sympy calls available.

Example:

```
>>> # Timeout after 3 seconds
>>> with timeout_after(3):
expr = simplify(expr)
```

Also added a `timeout` kwarg to `Expr.expand` and `expand` as an example
of how the manager can be used to apply to functions.

**caveats**

- I picked `_ask` as a good function to check, because it's called a lot. However, this may (probably will) add considerable overhead to sympy. The best option would be to find a set of functions that are called often enough, but not too often.

- This uses a global variable, and is thus not threadsafe. However, other things (`evaluate`, etc...) also use globals for the same effect, so sympy already isn't threadsafe.

- This doesn't allow for the resolution that #8295 does, but is *incredibly* easier to use.