Python:下载大文件时出现无法预测的内存错误 [英] Python: Unpredictable memory error when downloading large files

查看:279
本文介绍了Python:下载大文件时出现无法预测的内存错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个python脚本,我使用它从HTTP服务器下载大量的视频文件(每个50-400 MB)。迄今为止,在长时间的下载列表中工作良好,但由于某些原因,它很少有内存错误。



该机器有大约1 GB的RAM空闲,但我不认为在运行此脚本时RAM已经最大化。



我已经监视了任务管理器和perfmon中的内存使用情况,它总是与我所看到的一样:在下载过程中慢慢增加,然后返回完成下载后的正常级别(没有任何小的漏洞或者任何类似的漏洞)。



下载行为的方式是创建文件在0 KB,直到下载完成(或程序崩溃),然后它立即写入整个文件并关闭它。

 对于我的范围(len(urls)):
如果os.path.exists(folderName +'/'+文件名[i] +'.mov'):
打印'文件存在,继续。 '
继续

#请求下载页
req = urllib2.Request(urls [i],headers = headers)

sock = urllib2。 urlopen(req)
responseHeaders = sock.headers
body = sock.read()
sock.close()

#搜索页面下载URL
tmp = body.find('/ getfile /')
downloadSuffix = body [tmp:body.find('',tmp)]
downloadUrl = domain + downloadSuffix

req = urllib2.Request(downloadUrl,headers = headers)

打印'%s下载%s,%i'%b $ b%的文件%i(time.ctime()文件名[i],i + 1,len(urls))

f = urllib2.urlopen(req)

#打开我们的本地文件进行写入,'b'文件模式
video_file = open(foldername +'/'+文件名[i] +'.mov','wb')

#将下载的数据写入本地文件
video_file.write(f.read())##### MemoryError:内存不足#####
video_file.close()

打印'%s下载完成! '%(time.ctime())

#释放内存,希望防止内存错误
del f
del video_file

这是堆栈跟踪:

 文件downloadVideos.py,行159,在< module> 
main()
文件downloadVideos.py,第136行,主要
video_file.write(f.read())
文件c:\python27\ lib \socket.py,第358行,读取
buf.write(data)
MemoryError:内存不足


解决方案

您的问题在此: f.read()。该行尝试将整个文件下载到内存中。而不是读取块( chunk = f.read(4096)),并将这些片段保存到临时文件。


I wrote a python script which I am using to download a large number of video files (50-400 MB each) from an HTTP server. It has worked well so far on long lists of downloads, but for some reason it rarely has a memory error.

The machine has about 1 GB of RAM free, but I don't think it's ever maxed out on RAM while running this script.

I've monitored the memory usage in the task manager and perfmon and it always behaves the same from what I've seen: slowly increases during the download, then returns to normal level after it finishes the download (There's no small leaks that creep up or anything like that).

The way the download behaves is that it creates the file, which remains at 0 KB until the download finishes (or the program crashes), then it writes the whole file at once and closes it.

for i in range(len(urls)):
    if os.path.exists(folderName + '/' + filenames[i] + '.mov'):
        print 'File exists, continuing.'
        continue

    # Request the download page
    req = urllib2.Request(urls[i], headers = headers)

    sock = urllib2.urlopen(req)
    responseHeaders = sock.headers
    body = sock.read()
    sock.close()

    # Search the page for the download URL
    tmp = body.find('/getfile/')
    downloadSuffix = body[tmp:body.find('"', tmp)]
    downloadUrl = domain + downloadSuffix

    req = urllib2.Request(downloadUrl, headers = headers)

    print '%s Downloading %s, file %i of %i'
        % (time.ctime(), filenames[i], i+1, len(urls))

    f = urllib2.urlopen(req)

    # Open our local file for writing, 'b' for binary file mode
    video_file = open(foldername + '/' + filenames[i] + '.mov', 'wb')

    # Write the downloaded data to the local file
    video_file.write(f.read()) ##### MemoryError: out of memory #####
    video_file.close()

    print '%s Download complete!' % (time.ctime())

    # Free up memory, in hopes of preventing memory errors
    del f
    del video_file

Here is the stack trace:

  File "downloadVideos.py", line 159, in <module>
    main()
  File "downloadVideos.py", line 136, in main
    video_file.write(f.read())
  File "c:\python27\lib\socket.py", line 358, in read
    buf.write(data)
MemoryError: out of memory

解决方案

Your problem is here: f.read(). That line attempts to download the entire file into memory. Instead of that, read in chunks (chunk = f.read(4096)), and save the pieces to temporary file.

这篇关于Python:下载大文件时出现无法预测的内存错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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