当多线程python进程运行时,OS开始终止进程

2024-09-27 00:18:11 发布

您现在位置:Python中文网/ 问答频道 /正文

这是最奇怪的事!在

我有一个用Python编写的多线程客户机应用程序。我使用线程来同时下载和处理页面。我将使用cURL多句柄,除了瓶颈肯定是这个应用程序中的处理器(而不是带宽),因此使用线程池更有效。在

我有一个64b i7摇摆16GB内存。比菲。我启动80个线程,同时听潘多拉和拖拉Stackoverflow和BAM!父进程有时以消息结尾

Killed

其他时候,一个单独的页面(在Chrome中是它自己的进程)会死掉。有时整个浏览器崩溃。在

如果你想看一点代码,这里是它的要点:

下面是父进程:

def start( ):
  while True:
    for url in to_download:
      queue.put( ( url, uri_id ) )

    to_download = [ ]

    if queue.qsize( ) < BATCH_SIZE:
      to_download = get_more_urls( BATCH_SIZE )

    if threading.activeCount( ) < NUM_THREADS:
      for thread in threads:
        if not thread.isAlive( ):
          print "Respawning..."
          thread.join( )
          threads.remove( thread )
          t = ClientThread( queue )
          t.start( )
          threads.append( t )

    time.sleep( 0.5 )

下面是ClientThread的要点:

^{pr2}$

编辑:哦,对了……忘了问这个问题……应该很明显:为什么我的进程会被杀死?它是否发生在操作系统级别?内核级别?这是因为对我可以拥有的打开的TCP连接数量的限制吗?这是对我一次可以运行的线程数的限制吗?cat /proc/sys/kernel/threads-max的输出是257841。所以…我不认为是。。。。在

我想我找到了…好吧…我的硬盘上根本没有交换空间。现在有没有办法创建一些交换空间?我在运行Fedora16。有交换…然后我启用了我所有的RAM,它神奇地消失了。拖尾/var/log/messages我发现了这个错误:

Mar 26 19:54:03 gazelle kernel: [700140.851877] [15961]   500 15961    12455     7292   1       0             0 postgres
Mar 26 19:54:03 gazelle kernel: [700140.851880] Out of memory: Kill process 15258 (chrome) score 5 or sacrifice child
Mar 26 19:54:03 gazelle kernel: [700140.851883] Killed process 15258 (chrome) total-vm:214744kB, anon-rss:70660kB, file-rss:18956kB
Mar 26 19:54:05 gazelle dbus: [system] Activating service name='org.fedoraproject.Setroubleshootd' (using servicehelper)

Tags: to应用程序ifqueue进程download页面线程
2条回答

你在做一个

raise SystemExit

它实际上退出了Python解释器,而不是运行在其中的线程。在

您已经触发了内核的内存不足(OOM)处理程序;它以一种复杂的方式选择要杀死哪些进程,会尽力杀死尽可能少的进程以产生最大的影响。根据内核使用的标准,Chrome显然是最有吸引力的进程。在

您可以在/proc/[pid]/oom_score文件下的proc(5)手册页中看到条件的摘要:

   /proc/[pid]/oom_score (since Linux 2.6.11)
          This file displays the current score that the kernel
          gives to this process for the purpose of selecting a
          process for the OOM-killer.  A higher score means that
          the process is more likely to be selected by the OOM-
          killer.  The basis for this score is the amount of
          memory used by the process, with increases (+) or
          decreases (-) for factors including:

          * whether the process creates a lot of children using
            fork(2) (+);

          * whether the process has been running a long time, or
            has used a lot of CPU time (-);

          * whether the process has a low nice value (i.e., > 0)
            (+);

          * whether the process is privileged (-); and

          * whether the process is making direct hardware access
            (-).

          The oom_score also reflects the bit-shift adjustment
          specified by the oom_adj setting for the process.

如果希望Python程序的oom_score文件是被杀死的,那么可以为它调整该文件。在

可能更好的方法是在系统中添加更多的swap,以缩短OOM killer被调用的时间。当然,有更多的交换并不一定意味着你的系统永远不会耗尽内存,如果有大量交换流量,你可能不关心它的处理方式,但它至少可以让你克服内存紧张的问题。在

如果已经为交换分区分配了所有可用空间,那么可以添加swap文件。因为它们要经过文件系统,交换文件的开销要比交换分区多,但您可以在驱动器分区后添加它们,这是一个简单的短期解决方案。您可以使用dd(1)命令来分配文件(不要使用seek来生成一个稀疏文件),然后使用mkswap(8)格式化该文件以供交换使用,然后使用swapon(8)打开该特定文件。(我想您甚至可以将交换文件添加到fstab(5)中,以便在下次重新启动时自动使用它们,但我从未尝试过,也不知道其语法。)

相关问题 更多 >

    热门问题