杀死线程并在Python中释放内存

时间:2015-02-18 23:49:56

标签: python multithreading garbage-collection

我正在杀死这里显示的线程:Is there any way to kill a Thread in Python?

但是我注意到,内存没有被释放(gc.get_objects()不断增长和增长)。事实上,这些对象是列表,序列等,而不是文件。

我有没有办法手动释放资源? 代码:

import ctypes

def terminate_thread(thread):
    """Terminates a python thread from another thread.

    :param thread: a threading.Thread instance
    """
    if not thread.isAlive():
        return

    exc = ctypes.py_object(SystemExit)
    res = ctypes.pythonapi.PyThreadState_SetAsyncExc(
        ctypes.c_long(thread.ident), exc)
    if res == 0:
        raise ValueError("nonexistent thread id")
    elif res > 1:
        # """if it returns a number greater than one, you're in trouble,
        # and you should call it again with exc=NULL to revert the effect"""
        ctypes.pythonapi.PyThreadState_SetAsyncExc(thread.ident, None)
        raise SystemError("PyThreadState_SetAsyncExc failed")

class MyThread(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.result = None
        self.error = None

    def run(self):
        try:
            self.result = myfun(*args, **kw) #run external resource and the interrupt it
        except Exception as e:
            self.error = e

c = MyThread()
c.start()
c.join(60) # wait a minute
counter = 0
if c.isAlive():
    while c.isAlive():
        time.sleep(0.1)
        try:
            terminate_thread(c) # how to release resources?
        except:
            break
        counter += 1
        if counter > 10: break
    raise TimeoutException

输出示例:

print('Controlled objects: %s' % len(gc.get_objects()))
print ('Unreachable: %s' % gc.collect())

Controlled objects: 85084 #request 1, no timeout
Unreachable: 5640

Controlled objects: 171994 # request 2, timeout
Unreachable: 7221

1 个答案:

答案 0 :(得分:3)

好了所有这些垃圾之后我认为你想要的是多处理模块,因为我相信你实际上可以发送一个sigkill

class MyThread:
    def __init__(self):
        self.result = None
        self.error = None
    def start(self):
        self.proc = multiprocessing.Process(target=self.run)
        self.proc.start()
    def stop(self):
       self.proc.send_signal(multiprocessing.SIG_KILL)
    def run(self):
        try:
            self.result = myfun(*args, **kw) #run external resource and the interrupt it
        except Exception as e:
            self.error = e

然后你会调用c.stop()以便用sig_kill来停止线程(粗略的,其他东西应该对此做出适当的反应)

您甚至可以使用内置subprocess.Process.kill()(请参阅文档https://docs.python.org/2/library/subprocess.html#subprocess.Popen.send_signal

写下你的问题 ##

(我有没有办法手动释放资源?)

 t = Thread(target=some_long_running_external_process)
 t.start()

无法从t

之外退出您的主题(some_long_running_external_process