繁体   English   中英

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

[英]Killing Thread and releasing memory in Python

我正在杀死这里显示的线程: 有没有办法在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

好吧,在所有这些垃圾之后,我认为你想要的是多处理模块,因为我相信你实际上可以发送一个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来停止线程(粗略的,另一个应该对此做出适当的响应)

你甚至可以使用builtin 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()

没有办法从some_long_running_external_process外部退出你的线程( t

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM