macOS,是否可以终止单个python线程?

时间:2019-01-11 09:33:28

标签: python python-3.x terminate

我正在Jupyter笔记本上运行长时间的计算,并且由python产生的一个线程(我怀疑是pickle.dump调用)占用了所有可用的RAM,使系统笨拙。

现在,我想终止单线程。中断笔记本电脑不起作用,我不想重启笔记本电脑,以免丢失到目前为止所做的所有计算。如果我打开活动监视器,则可以清楚地看到一个包含多个线程的python进程。

我知道我可以终止整个过程,但是有没有办法终止单个线程?

2 个答案:

答案 0 :(得分:1)

我认为您无法杀死进程本身之外的进程线程:

this answer

中所述
  

线程是进程不可或缺的一部分,不能被杀死   在它外面。有pthread_kill函数,但仅适用于   thread itself的上下文。来自链接上的文档

答案 1 :(得分:0)

当然答案是肯定的,我有一个演示代码FYI(不安全):

// Create a string array with the lines of text
string[] lines = File.ReadAllLines(path-of-file);

// Write the string array to a new file named "ouput.xml".
using (StreamWriter outputFile = new StreamWriter(Path.Combine(mydocpath,"output.xml"))) {
    foreach (string line in lines)
        outputFile.WriteLine("<!--" + line + "-->");
}