gpt4 book ai didi

python - Python中杀死线程并释放内存

转载 作者:行者123 更新时间:2023-11-30 23:14:50 29 4
gpt4 key购买 nike

我正在杀死线程,如下所示: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

最佳答案

好吧,在所有这些垃圾之后,我认为你想要的是多处理模块,因为我相信你实际上可以在上面发送一个 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()

无法从 some_long_running_external_process 外部退出线程 (t)

关于python - Python中杀死线程并释放内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28596212/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com