Python:下载大文件时出现不可预测的内存错误
我编写了一个python脚本,用于从HTTP服务器下载大量视频文件(每个50-400 MB)。到目前为止,它在长长的下载列表上运行良好,但由于某些原因,它很少出现内存错误 这台机器大约有1GB的可用内存,但我认为在运行这个脚本时,它的内存从来没有达到最大值 我已经在任务管理器和perfmon中监控了内存使用情况,它的行为与我所看到的一样:在下载过程中缓慢增加,然后在下载完成后恢复到正常水平(没有小的泄漏或类似的情况) 下载的行为方式是,它创建文件,在下载完成(或程序崩溃)之前保持0 KB,然后立即写入整个文件并关闭它Python:下载大文件时出现不可预测的内存错误,python,download,out-of-memory,Python,Download,Out Of Memory,我编写了一个python脚本,用于从HTTP服务器下载大量视频文件(每个50-400 MB)。到目前为止,它在长长的下载列表上运行良好,但由于某些原因,它很少出现内存错误 这台机器大约有1GB的可用内存,但我认为在运行这个脚本时,它的内存从来没有达到最大值 我已经在任务管理器和perfmon中监控了内存使用情况,它的行为与我所看到的一样:在下载过程中缓慢增加,然后在下载完成后恢复到正常水平(没有小的泄漏或类似的情况) 下载的行为方式是,它创建文件,在下载完成(或程序崩溃)之前保持0 KB,然后立
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
以下是堆栈跟踪:
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
文件“downloadVideos.py”,第159行,在
main()
文件“downloadVideos.py”,第136行,主目录
video_file.write(f.read())
文件“c:\python27\lib\socket.py”,第358行,已读
buf.写入(数据)
内存错误:内存不足
您的问题就在这里:f.read()
。该行尝试将整个文件下载到内存中。取而代之的是,读取数据块(chunk=f.read(4096)
),并将数据块保存到临时文件中。您如何知道下载完成了?还是整个数据的长度?文档中根本没有关于f.read()
返回的对象的任何信息。您需要查看内容长度
标题。