2011-03-10 11 views
6

Estoy trabajando en un entorno de memoria limitada donde necesito crear archivos de vuelcos de SQL. Si utilizo python's built en tarfile module, ¿el archivo '.tar' se guarda en la memoria o se escribe en el disco a medida que se crea?¿El módulo `tarfile` de Python almacena los archivos que está construyendo en la memoria?

Por ejemplo, en el siguiente código, si huge_file.sql tiene 2GB, la variable tar ocupará 2GB en la memoria?

import tarfile 

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

Respuesta

5

No, no lo está cargando en la memoria. Puede leer el source for tarfile ver que se trata de utilizar copyfileobj, que está utilizando un buffer de tamaño fijo de copia del archivo en el archivo comprimido:

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

+1 para ligarse a la fuente. Los documentos de desarrollo ahora también tienen un enlace a las fuentes http://docs.python.org/dev/library/tarfile – jfs

Cuestiones relacionadas