简体   繁体   English

Windows下如何限制Python中的内存和CPU使用率?

[英]How to limit memory and CPU usage in Python under Windows?

I write deep learning software using Python and the Tensorflow library under Windows. 我使用Windows下的Python和Tensorflow库编写深度学习软件。 Sometimes by mistake I load too much into memory and the computer stops responding; 有时我会错误地将太多的内容加载到内存中,并且计算机停止响应; i cannot even kill the process. 我什至无法杀死这个过程。

Is it possible to limit the memory and CPU usage for Python scripts under Windows? 是否可以限制Windows下Python脚本的内存和CPU使用率? I use PyCharm as an editor. 我使用PyCharm作为编辑器。 Under UNIX Systems there seems to be the possibility to use resource.RLIMIT_VMEM , but under Windows I get the notification no module named resource . 在UNIX系统下,似乎有可能使用resource.RLIMIT_VMEM ,但是在Windows下,我收到通知, no module named resource

You can, of course, use the Win32 Jobs API (CreateJobObject & AssignProcessToJobObject) to spawn your program as a sub-process and manage its resources. 当然,您可以使用Win32 Jobs API(CreateJobObject和AssignProcessToJobObject)将程序作为子进程生成并管理其资源。

But I guess a simpler solution, without going through all the hassle of coding, is to use Docker to create a managed environment. 但是我想,一个简单的解决方案是使用Docker创建托管环境,而无需经历所有编码麻烦。

This is a common problem when running resource-intensive processes, where the total amount of memory required might be hard to predict. 当运行资源密集型进程时,这是一个常见问题,在该进程中可能很难预测所需的内存总量。

If the main issue is the whole system halting, you can create a watchdog process preventing that from happening and killing the process. 如果主要问题是整个系统停止运行,则可以创建一个监视程序,以防止该情况发生并终止该进程。 It is a bit hacky, not as clean as the UNIX solution, and it will cost you a bit of overhead, but at least it can save you a restart! 它有点笨拙,不如UNIX解决方案那么干净,并且会花费您一些开销,但至少可以节省您的重新启动!

This can easily be done in python, using the psutil package. 使用psutil包,可以在python中轻松完成此操作。 This short piece of code runs whenever over 90% of virtual memory has been used and kills the python.exe process which is using the most memory: 只要使用了90%以上的虚拟内存,这段简短的代码就会运行,并杀死占用最多内存的python.exe进程:

import time
import psutil
while True:
    if psutil.virtual_memory().percent > 90:
        processes = []
        for proc in psutil.process_iter():
            if proc.name() == 'python.exe':
                processes.append((proc, proc.memory_percent()))
        sorted(processes, key=lambda x: x[1])[-1][0].kill()
    time.sleep(10)

This can also be adapted for CPU, using psutil.cpu_percent() . 也可以使用psutil.cpu_percent()将其调整为适用于CPU。

It's a bad practice to limit the CPU usage. 限制CPU使用率是一种不好的做法。 You're doing something wrong, fix it instead of working around it. 您做错了什么,请修复它,而不要解决它。

You said: 你说:

Sometimes by mistake I load too much into memory and the computer stops responding; 有时我会错误地将太多的内容加载到内存中,并且计算机停止响应;

What do you exactly mean by mistake ? 你到底是什么意思呢? You should learn from your mistakes. 您应该从错误中学习。

I'd suggest running the heavy processing part in a separate thread so your script wont stop responding. 我建议在一个单独的线程中运行繁重的处理部分,以便您的脚本不会停止响应。

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

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