当我使用python在linux中终止时,我找到了以os.system(' kill -9 {0}' .format(pid))的方式关闭进度的方法,但它失败。
我的代码是运行两个进度。当第一个进度完成时,我希望第二个进度也关闭(第二个进度是它自动运行,如果你不关闭它)。< / p>
sys.setrecursionlimit(1000000)
progress1 = Process(target=main)
progress2 = Process(target=run_proxypool)
progress1.daemon = True
progress2.start()
pid = progress2.pid
time.sleep(10)
progress1.start()
progress1.join()
os.system('kill -9 {0}'.format(pid))
答案 0 :(得分:0)
确保您在Linux上运行。另请考虑使用char **array; //array as been defined as global variable
#define FORMAT_STRING "%20s"
#define MAX_WORD_LENGTH 20
....
//allocate memory for array to store all the words
array = malloc(sizeof(char *)*n);
assert(array != NULL);
for(int i = 0; i < n; i ++) { //input the words
printf("Enter word: ");
array[i] = malloc(sizeof(char)*MAX_WORD_LENGTH);
assert(array[i] != NULL);
scanf(FORMAT_STRING, array[i]);
。
不完全是您所要求的,但考虑切换到psutil以获得跨平台优势以及wait_procs等便捷方式,它们将优雅地发送SIGTERM,等待,然后发送SIGKILL。
os.kill(pid, signal.SIGKILL)
应该可以在Linux上使用(不适用于Windows)。
多处理模块recently gained signal.SIGKILL
方法在Windows上回退到kill()
。
答案 1 :(得分:0)
来自https://clang.llvm.org/extra/doxygen/run-clang-tidy_8py_source.html:
# This is a sad hack. Unfortunately subprocess goes
# bonkers with ctrl-c and we start forking merrily.
print('\nCtrl-C detected, goodbye.')
if tmpdir:
shutil.rmtree(tmpdir)
os.kill(0, 9)
答案 2 :(得分:0)
如果您没有 PID,这里是您如何提取它并将其传递给 os.kill
import os, signal
def process(name):
try:
# iterating through each instance of the proess
for line in os.popen("ps ax | grep " + name + " | grep -v grep"):
fields = line.split()
# extracting Process ID from the output
pid = fields[0]
# terminating process
#os.kill(int(pid), signal.SIGKILL)
print("Process Successfully terminated")
except:
print("Error Encountered while running script")
process()