2011-03-10 4 views
6

SQL 덤프의 아카이브를 만들어야하는 메모리가 부족한 환경에서 작업하고 있습니다. 내가 파이썬을 tarfile module에 내장하면 '.tar'파일이 메모리에 저장되거나 디스크가 생성 될 때 디스크에 기록됩니까?파이썬의`tarfile` 모듈은 메모리에 저장하고있는 아카이브를 저장합니까?

예를 들어 다음 코드에서 huge_file.sql이 2GB이면 tar 변수가 메모리에서 2GB를 차지합니까?

import tarfile 

tar = tarfile.open("my_archive.tar.gz")), "w|gz") 
tar.add('huge_file.sql') 
tar.close() 

답변

5

아니요 메모리에로드되지 않습니다. 당신은 파일에서 타르볼로 복사 고정 된 크기의 버퍼를 사용하는 copyfileobj을 사용하고 있음을 확인하기 위해 source for tarfile 읽을 수 있습니다

def copyfileobj(src, dst, length=None): 
    """Copy length bytes from fileobj src to fileobj dst. 
     If length is None, copy the entire content. 
    """ 
    if length == 0: 
     return 
    if length is None: 
     shutil.copyfileobj(src, dst) 
     return 

    BUFSIZE = 16 * 1024 
    blocks, remainder = divmod(length, BUFSIZE) 
    for b in xrange(blocks): 
     buf = src.read(BUFSIZE) 
     if len(buf) < BUFSIZE: 
      raise IOError("end of file reached") 
     dst.write(buf) 

    if remainder != 0: 
     buf = src.read(remainder) 
     if len(buf) < remainder: 
      raise IOError("end of file reached") 
     dst.write(buf) 
    return 
+0

하나를 소스에 연결합니다. 개발 문서에는 http://docs.python.org/dev/library/tarfile에 대한 링크가 있습니다. – jfs

관련 문제