Python:ulimit非常适合subprocess.call/subprocess.Popen吗? [英] Python: ulimit and nice for subprocess.call / subprocess.Popen?

查看:166
本文介绍了Python:ulimit非常适合subprocess.call/subprocess.Popen吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要限制我使用subprocess.call从python进程生成的外部命令行应用程序占用的时间和cpu的数量,主要是因为有时生成的进程会卡住并将cpu固定为99%.

I need to limit the amount of time and cpu taken by external command line apps I spawn from a python process using subprocess.call , mainly because sometimes the spawned process gets stuck and pins the cpu at 99%.

好的和ulimit似乎是执行此操作的合理方法,但是我不确定它们如何与子流程进行交互.

nice and ulimit seem like reasonable ways to do this, but I'm not sure how they'd interact with subprocess.

  • 限制看起来像:
    • 杀死过程超过60秒
    • 将其限制为cpu的20%
    • The limits look something like:
      • Kill the process if it's taking more than 60 seconds
      • Limit it to 20% of cpu

      是否有一种方法可以将nice和ulimit应用于subprocess.call生成的进程?是否有更好的python-native替代品?

      Is there a way to apply nice and ulimit to the subprocess.call spawned process? Are there better python-native alternatives?

      这是在linux(ubuntu)系统上.

      This is on a linux (ubuntu) system.

      推荐答案

      您可以使用ulimitnice shell命令为子进程设置限制,如下所示:

      You can set limits for subprocesses with the ulimit and nice shell commands like this:

      import subprocess
      subprocess.Popen('ulimit -t 60; nice -n 15 cpuhog', shell=True)
      

      此命令运行cpuhog时,CPU时间限制为60秒,精细度调整为15.请注意,没有简单的方法可以这样设置20%的CPU油门.除非另一个(不太好的)进程也需要CPU,否则该进程将使用100%的CPU.

      This runs cpuhog with a limit of 60 seconds of CPU time and a niceness adjustment of 15. Note that there is no simple way to set a 20% CPU throttle as such. The process will use 100% CPU unless another (less nice) process also needs the CPU.

      这篇关于Python:ulimit非常适合subprocess.call/subprocess.Popen吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆