Par*_*and 39 python resources nice ulimit
我需要限制我使用subprocess.call从python进程生成的外部命令行应用程序所花费的时间和cpu的数量,主要是因为有时候生成的进程被卡住并将cpu的引脚设置为99%.
nice和ulimit似乎是合理的方法,但我不确定他们如何与子进程交互.
有没有办法将nice和ulimit应用于subprocess.call生成的进程?是否有更好的python-native替代品?
这是在linux(ubuntu)系统上.
Eri*_*erg 105
使用preexec_fn参数subprocess.Popen和资源模块.例:
parent.py:
#!/usr/bin/env python
import os
import sys
import resource
import subprocess
def setlimits():
# Set maximum CPU time to 1 second in child process, after fork() but before exec()
print "Setting resource limit in child (pid %d)" % os.getpid()
resource.setrlimit(resource.RLIMIT_CPU, (1, 1))
print "CPU limit of parent (pid %d)" % os.getpid(), resource.getrlimit(resource.RLIMIT_CPU)
p = subprocess.Popen(["./child.py"], preexec_fn=setlimits)
print "CPU limit of parent (pid %d) after startup of child" % os.getpid(), resource.getrlimit(resource.RLIMIT_CPU)
p.wait()
print "CPU limit of parent (pid %d) after child finished executing" % os.getpid(), resource.getrlimit(resource.RLIMIT_CPU)
Run Code Online (Sandbox Code Playgroud)
child.py:
#!/usr/bin/env python
import os
import sys
import resource
print "CPU limit of child (pid %d)" % os.getpid(), resource.getrlimit(resource.RLIMIT_CPU)
Run Code Online (Sandbox Code Playgroud)
parent.py将分叉到一个新进程.在新进程中,它将调用setlimits(),然后执行exec child.py.这意味着资源将在子进程中受限,但不在父进程中.
运行程序时输出:
./parent.py
CPU limit of parent (pid 17404) (-1, -1)
Setting resource limit in child (pid 17405)
CPU limit of parent (pid 17404) after startup of child (-1, -1)
CPU limit of child (pid 17405) (1, 1)
CPU limit of parent (pid 17404) after child finished executing (-1, -1)
Run Code Online (Sandbox Code Playgroud)
这在许多情况下是比尝试使用ulimit更好的解决方案,因为通过shell生成子进程并不总是一个好主意,特别是因为它经常导致丑陋的参数引用问题.
Vil*_*ari 11
您可以使用ulimit和niceshell命令为子进程设置限制,如下所示:
import subprocess
subprocess.Popen('ulimit -t 60; nice -n 15 cpuhog', shell=True)
Run Code Online (Sandbox Code Playgroud)
这将运行cpuhog时间限制为60秒的CPU时间和15的漂亮度调整.请注意,没有简单的方法可以设置20%的CPU节流.该过程将使用100%CPU,除非另一个(不太好)的过程也需要CPU.
Erik让我很容易,但他忘记了Rich指出的nice部分.我发现包装很好(双关语),但遗憾的是不太便携.以下是我对这个问题的看法:psutil
import os
import psutil
import resource
import subprocess
def preexec_fn():
pid = os.getpid()
ps = psutil.Process(pid)
ps.set_nice(10)
resource.setrlimit(resource.RLIMIT_CPU, (1, 1))
print "mother pid", os.getpid()
p = subprocess.Popen(["./cpuhog.sh"], preexec_fn=preexec_fn)
p.wait()
print "mother still alive with pid", os.getpid()
Run Code Online (Sandbox Code Playgroud)
Ville使用了shell=True我某种过敏的东西.也许我在这里只是老了,脾气暴躁,但我尽量避免它!
| 归档时间: |
|
| 查看次数: |
23630 次 |
| 最近记录: |