Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly is Python's SpooledTemporaryFile?

I was looking into the tempfile options in Python, when I ran into SpooledTemporaryFile, now, the description says:

This function operates exactly as TemporaryFile() does, except that data is spooled in memory until the file size exceeds max_size, or until the file’s fileno() method is called, at which point the contents are written to disk and operation proceeds as with TemporaryFile().

I would like to understand exactly what that means, I have looked around but found no answer, if I get it right:

Is the written data 'buffered' in RAM until it reaches a certain threshold and then saved in disk? What is the advantage of this method over the standard approach? Is it faster? Because in the end it will have to be saved in disk anyway...

Anyway, if anyone can provide me with some insights I'd be grateful.

like image 591
Lucas Abbade Avatar asked Dec 18 '19 13:12

Lucas Abbade


1 Answers

The data is buffered in memory before writing. The question becomes, will anything be written?

Not necessarily. TemporaryFile, by default, deletes the file after it is closed. If a spooled file never gets big enough to write to disk (and the user never tries to invoke its fileno method), the buffer will simply be flushed when it is closed, with no actual disk I/O taking place.

like image 183
chepner Avatar answered Nov 14 '22 21:11

chepner