如何从GPU内存中删除python进程?

2024-03-29 06:18:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图运行一个函数,但过早地终止了它。我尝试使用Python的多处理库为函数创建一个进程,然后终止它。我还尝试使用多处理库来终止所有子进程。我的代码运行时不显示错误消息。然而,即使程序已经完全终止(不仅仅是进程,而是整个过程),我的GPU的内存也被耗尽了。我不知道为什么会这样。任何帮助都将不胜感激。你知道吗

这是我的密码:

class TrainingHelper:

    def __init__(self, filename = "nah.txt"):
        #filename is the name of the log file
        self.filename = filename

    def train(self):
        import reaver as rvr
        env = rvr.envs.SC2Env(map_name='FindAndDefeatZerglings')
        agent = rvr.agents.A2C(env.obs_spec(), env.act_spec(), 
        rvr.models.build_fully_conv, rvr.models.SC2MultiPolicy, 
        n_envs=4)
        agent.run(env)


    def run(self):
        import multiprocessing
        import time
        import sys
        p = multiprocessing.Process(target=self.train, name="Reaver", 
        args=())


        p.start()
        time.sleep(120)
        p.terminate()
        p.join()

        for prc in multiprocessing.active_children():
            prc.terminate()


        print("hello") #this works



if __name__ == "__main__":
    trainingHelper = TrainingHelper()
    trainingHelper.run()

下面是运行命令nvidiasmi的屏幕截图

Here's a screenshot from running the command nvidia-smi


Tags: the函数runnameimportselfenv进程