这是最奇怪的事情!
我有一个用 Python 编写的多线程客户端应用程序。我正在使用线程同时下载和处理页面。我会使用 cURL 多句柄,除了瓶颈肯定是此应用程序中的处理器(而不是带宽),因此使用线程池更有效。
我有一个 64b i7 摇摆 16GB RAM。仡。我在听 Pandora 并拖钓 Stackoverflow 和 BAM 的同时启动了 80 个线程!父进程有时会以消息结束
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 的要点:
class ClientThread( threading.Thread ):
def __init__( self, queue ):
threading.Thread.__init__( self )
self.queue = queue
def run( self ):
while True:
try:
self.url, self.url_id = self.queue.get( )
except:
raise SystemExit
html = StringIO.StringIO( )
curl = pycurl.Curl( )
curl.setopt( pycurl.URL, self.url )
curl.setopt( pycurl.NOSIGNAL, True )
curl.setopt( pycurl.WRITEFUNCTION, html.write )
curl.close( )
try:
curl.perform( )
except pycurl.error, error:
errno, errstr = error
print errstr
curl.close( )
编辑:哦,对了......忘了问这个问题......应该很明显:为什么我的进程会被杀死?它发生在操作系统级别吗?内核级别?这是由于我可以拥有的打开 TCP 连接数的限制吗?我一次可以运行的线程数是否有限制?的输出cat /proc/sys/kernel/threads-max
是257841
。所以……我不认为是这样……
我想我明白了……好吧……我的驱动器上根本没有交换空间。现在有没有办法创建一些交换空间?我正在运行 Fedora 16。有交换...然后我启用了我所有的 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)