简体   繁体   English

Python进程在Ubuntu上消亡:错误代码是什么意思,是否有修复程序?

[英]Python process dies on Ubuntu: what does the error code mean and is there a fix?

I am running multiple copies of the same python script on an Amazon EC2 Ubuntu instance. 我正在Amazon EC2 Ubuntu实例上运行同一Python脚本的多个副本。 Each copy in turn launches the same child Python script using the solution proposed here 每个副本依次使用此处提出的解决方案启动相同的子Python脚本

From time to time some of these child processes die. 这些子进程有时会死掉。 subprocess.check_output throws an exception and returns the error code -9 . subprocess.check_output引发异常并返回错误代码-9 I ran the child process directly from the prompt and after running for some time, the process dies with a not-so-detailed message Killed . 我直接从提示符下运行子进程,运行了一段时间后,该进程死于一条不太详细的消息Killed

Questions: 问题:

  • What does -9 mean? -9是什么意思?
  • How can I find out more about what went wrong? 我如何才能找到更多有关发生问题的信息? Specifically, my suspicion is that it might be caused by the machine getting overloaded by the several copies of the same script running at the same time. 具体来说,我怀疑这可能是由于同一时间运行的同一脚本的多个副本使计算机过载而导致的。 At the same time, the specific child process that I ran directly appears to be dying every time it's launched, directly or not, and more or less at the same moment (ie after processing more or less the same amount of input data). 同时,我直接运行的特定子进程似乎在每次启动时都快要死了,无论是直接启动还是不启动,并且几乎在同一时间(即在处理或多或少相同数量的输入数据之后)都将消失。 Python is not producing any error messages. Python不会产生任何错误消息。
  • Assuming I have no bugs in the Python code, what can I do to try to prevent the crashes? 假设我在Python代码中没有错误,该怎么做才能防止崩溃?

check_output() accumulates output from the subprocess in memory. check_output()将子check_output()输出累积到内存中。 If the process generates enough output it might be killed by oom killer due to the large RAM consumption. 如果该进程产生足够的输出,则可能由于大量RAM消耗而被oom killer杀死。

If you don't need the output, you could use check_call() instead and discard the output: 如果不需要输出,则可以改用check_call()并丢弃输出:

import os
from subprocess import check_call, STDOUT

DEVNULL = open(os.devnull, "r+b")

check_call([command], stdout=DEVNULL, stderr=STDOUT)

-9 means kill signal that is not catchable or ignorable, or just quit immediately. -9表示无法捕获或忽略或直接退出的杀死信号。

For example if you're trying to kill a process you could enter in your terminal: 例如,如果您要终止进程,则可以在终端中输入:

ps aux | grep processname

or just this to get a list of all processes: ps aux 或仅此而获得所有进程的列表: ps aux

Once you have the pid of the process you want to terminate, you'd type kill -9 followed by the pid: 获得要终止的进程的pid之后,请键入kill -9,然后键入pid:

kill -9 1234 

My memory is a little foggy when it comes to logs, but I'd cat around in /var/log/ and see if you find anything, or dmesg. 当涉及到日志时,我的记忆有些模糊,但是我会在/ var / log /中查看一下,看看是否找到任何东西,或者是dmesg。

As far as preventing crashes in your Python code, have you tried any exception handling? 至于防止Python代码崩溃,您是否尝试过任何异常处理?

Exceptions in Python Python例外

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

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