Mike Driscoll
Mike Driscoll

@driscollis

5 Tweets 5 reads Jan 16, 2023
Have you ever needed to kill โ˜ ๏ธ a subprocess in #Python?
Let's talk a little about that a little!
๐Ÿงต๐Ÿ๐Ÿ‘‡
I had a use-case where I would spin up a process using `subprocess` that I wanted to kill if it ran too long
One way to do that is to use #Python's `threading` module, which has a handy `Timer` class
In this example, the `Timer` class is set to die after 5 seconds
#Python's `subprocess` module has a `run()` function that includes a `timeout` parameter (starting in 3.5) that you can use as well.
Here's an example of how that would work:
Both of these examples are talked about in a tutorial on @mousevspython here:
blog.pythonlibrary.org
I hope you enjoyed the mini-thread on killing #Python subprocesses
Follow me for more great Python conten!

Loading suggestions...