How to kill a subprocess called using subprocess.call in python? [duplicate]

2024/10/12 11:22:05

I am calling a command using

subprocess.call(cmd, shell=True)

The command won't terminate automatically. How to kill it?

Answer

Since subprocess.call waits for the command to complete, you can't kill it programmatically. Your only recourse is to kill it manually via an OS specific command like kill.

If you want to kill a process programmatically, you'll need to start the process with subprocess.Popen and then terminate it. An example of this is below:

import time, subprocesst1 = time.time()p = subprocess.Popen('sleep 1', shell=True)
p.terminate()
p.wait()
t2 = time.time()print(t2 - t1)

This script takes about .002s to execute (rather than ~1s if the sleep command wasn't terminated).

https://en.xdnf.cn/q/69658.html

Related Q&A

Printing one color using imshow [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Send headers along in python [duplicate]

This question already has answers here:Changing user agent on urllib2.urlopen(9 answers)Closed 9 years ago.I have the following python script and I would like to send "fake" header informatio…

How to download image to memory using python requests?

Like here How to download image using requests , but to memory, using http://docs.python-requests.org.

Accessing NumPy record array columns in Cython

Im a relatively experienced Python programmer, but havent written any C in a very long time and am attempting to understand Cython. Im trying to write a Cython function that will operate on a column o…

scipy append all rows of one sparse matrix to another

I have a numpy matrix and want to append another matrix to that.The two matrices have the shapes:m1.shape = (2777, 5902) m2.shape = (695, 5902)I want to append m2 to m1 so that the new matrix is of sh…

Add argparse arguments from external modules

Im trying to write a Python program that could be extended by third parties. The program will be run from the command line with whatever arguments are supplied.In order to allow third parties to creat…

Cosine similarity for very large dataset

I am having trouble with calculating cosine similarity between large list of 100-dimensional vectors. When I use from sklearn.metrics.pairwise import cosine_similarity, I get MemoryError on my 16 GB ma…

What exactly are the csv modules Dialect settings for excel-tab?

The csv module implements classes to read and write tabular data in CSV format. It allows programmers to say, “write this data in the formatpreferred by Excel,” or “read data from this file which wa…

Python: how to make a recursive generator function

I have been working on generating all possible submodels for a biological problem. I have a working recursion for generating a big list of all the submodels I want. However, the lists get unmanageably …

Change default options in pandas

Im wondering if theres any way to change the default display options for pandas. Id like to change the display formatting as well as the display width each time I run python, eg:pandas.options.display.…