How to stop long-running code in Python
Ever had long-running code that you don't know when it's going to finish running? If you have, then Python's stopit library is for you. In a previous post, we talked about how to create a progress bar to monitor Python code. This post will show you how to automatically stop long-running code with the stopit package. Getting started with stopit To get started with stopit, you can install it via pip: In our first example, we'll use a context manager to stop the code we want to execute after a timeout limit is reached. The result of running the above code will print out "DID NOT FINISH...". Above, we just need to specify the number of seconds we want the timeout limit to be - in this case, 5. Inside…







