为什么我的 python 脚本会随机被杀死?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1811173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:06:15  来源:igfitidea点击:

Why does my python script randomly get killed?

pythonmysqlurl

提问by TIMEX

Basically, i have a list of 30,000 URLs. The script goes through the URLs and downloads them (with a 3 second delay in between). And then it stores the HTML in a database.

基本上,我有一个包含 30,000 个 URL 的列表。该脚本遍历 URL 并下载它们(中间有 3 秒的延迟)。然后它将 HTML 存储在数据库中。

And it loops and loops...

它循环和循环......

Why does it randomly get "Killed."? I didn't touch anything.

为什么它会随机被“杀死”?我什么都没碰。

Edit: this happens on 3 of my linux machines. The machines are on a Rackspace cloud with 256 MB memory. Nothing else is running.

编辑:这发生在我的 3 台 linux 机器上。这些机器位于具有 256 MB 内存的 Rackspace 云上。没有其他东西在运行。

回答by Alex Martelli

Looks like you might be running out of memory -- might easily happen on a long-running program if you have a "leak" (e.g., due to accumulating circular references). Does Rackspace offer any easily usable tools to keep track of a process's memory, so you can confirm if this is the case? Otherwise, this kind of thing is not hard to monitor with normal Linux tools from outside the process. Once you have determined that "out of memory" is the likely cause of death, Python-specific tools such as pymplercan help you track exactly where the problem is coming from (and thus determine how to avoid those references -- be it by changing them to weak references, or other simpler approaches -- or otherwise remove the leaks).

看起来您可能会耗尽内存——如果您有“泄漏”(例如,由于累积循环引用),则很容易在长时间运行的程序上发生。Rackspace 是否提供任何易于使用的工具来跟踪进程的内存,以便您确认是否是这种情况?否则,这种事情不难用普通的 Linux 工具从进程外监控。一旦确定“内存不足”可能是导致死亡的原因,pympler等特定于 Python 的工具可以帮助您准确跟踪问题的来源(从而确定如何避免这些引用——无论是通过更改弱引用,或其他更简单的方法——或以其他方式消除泄漏)。

回答by steveha

In cases like this, you should check the log files.

在这种情况下,您应该检查日志文件。

I use Debian and Ubuntu, so the main log file for me is: /var/log/syslog

我使用 Debian 和 Ubuntu,所以我的主要日志文件是: /var/log/syslog

If you use Red Hat, I think that log is: /var/log/messages

如果您使用 Red Hat,我认为该日志是: /var/log/messages

If something happens that is as exceptional as the kernel killing your process, there willbe a log event explaining it.

如果发生的某些事是罕见,因为内核杀死你的过程中,日志事件解释它。

I suspect you are being hit by the Out Of Memory Killer.

我怀疑您正在被Out Of Memory Killer击中。

回答by Graeme Perrow

Is it possible that it's hitting an uncaught exception? Are you running this from a shell, or is it being run from cron or in some other automated way? If it's automated, the output may not be displayed anywhere.

它是否有可能遇到未捕获的异常?您是从 shell 运行它,还是从 cron 或其他一些自动化方式运行它?如果它是自动化的,则输出可能不会显示在任何地方。

回答by Stefano Borini

Are you using some sort of queue manager or process manager of some sort ? I got apparently random killed messages when the batch queue manager I was using was sending SIGUSR2 when the time was up.

您是否在使用某种队列管理器或某种进程管理器?当我使用的批处理队列管理器在时间到时发送 SIGUSR2 时,我显然收到了随机终止的消息。

Otherwise I strongly favor the out of memory option.

否则我强烈支持内存不足选项。

回答by shellbye

For those who came here with mysql, I found this answers may by helpful:

对于那些带着 来到这里的人mysql,我发现这个答案可能会有所帮助:

use SSCursoras suggented by this

使用SSCursor由作为suggented

conn = MySQLdb.connect(host=DB_HOST, user=DB_USER, db=DB_NAME,
                       passwd=DB_PASSWORD, charset="utf8",
                       cursorclass=MySQLdb.cursors.SSCursor)

and iterate over cursor as suggested by this

并按照this的建议迭代光标

cursor = conn.cursor()
cursor.execute("select * from very_big_table;")    
for row in cur:
    # do what you want here
    pass

Do pay attention to what the docsays You MUST retrieve the entire result set and close() the cursor before additional queries can be peformed on the connection., so if you want write and the same time, you should use another connection, or you will get

请注意文档所说的内容You MUST retrieve the entire result set and close() the cursor before additional queries can be peformed on the connection.,因此如果您想同时写入,则应使用另一个连接,否则会得到

`_mysql_exceptions.ProgrammingError: (2014, "Commands out of sync; you can't run this command now")`