- FlushFileBuffers function (fileapi.h)
- Syntax
- Parameters
- Return value
- Remarks
- FlushViewOfFile function (memoryapi.h)
- Syntax
- Parameters
- Return value
- Remarks
- File Stream. Flush Метод
- Определение
- Перегрузки
- Flush()
- Исключения
- Примеры
- Комментарии
- What exactly is Python’s file.flush() doing?
- 4 Answers 4
- How to flush all file buffers in Windows?
- 2 Answers 2
FlushFileBuffers function (fileapi.h)
Flushes the buffers of a specified file and causes all buffered data to be written to a file.
Syntax
Parameters
A handle to the open file.
The file handle must have the GENERIC_WRITE access right. For more information, see File Security and Access Rights.
If hFile is a handle to a communications device, the function only flushes the transmit buffer.
If hFile is a handle to the server end of a named pipe, the function does not return until the client has read all buffered data from the pipe.
Return value
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
The function fails if hFile is a handle to the console output. That is because the console output is not buffered. The function returns FALSE, and GetLastError returns ERROR_INVALID_HANDLE.
Remarks
Typically the WriteFile and WriteFileEx functions write data to an internal buffer that the operating system writes to a disk or communication pipe on a regular basis. The FlushFileBuffers function writes all the buffered information for a specified file to the device or pipe.
Due to disk caching interactions within the system, the FlushFileBuffers function can be inefficient when used after every write to a disk drive device when many writes are being performed separately. If an application is performing multiple writes to disk and also needs to ensure critical data is written to persistent media, the application should use unbuffered I/O instead of frequently calling FlushFileBuffers. To open a file for unbuffered I/O, call the CreateFile function with the FILE_FLAG_NO_BUFFERING and FILE_FLAG_WRITE_THROUGH flags. This prevents the file contents from being cached and flushes the metadata to disk with each write. For more information, see CreateFile.
To flush all open files on a volume, call FlushFileBuffers with a handle to the volume. The caller must have administrative privileges. For more information, see Running with Special Privileges.
When opening a volume with CreateFile, the lpFileName string should be the following form: \.\x: or \?\Volume<GUID>. Do not use a trailing backslash in the volume name, because that indicates the root directory of a drive.
In WindowsВ 8 and Windows ServerВ 2012, this function is supported by the following technologies.
FlushViewOfFile function (memoryapi.h)
Writes to the disk a byte range within a mapped view of a file.
Syntax
Parameters
A pointer to the base address of the byte range to be flushed to the disk representation of the mapped file.
The number of bytes to be flushed. If dwNumberOfBytesToFlush is zero, the file is flushed from the base address to the end of the mapping.
Return value
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
Remarks
Flushing a range of a mapped view initiates writing of dirty pages within that range to the disk. Dirty pages are those whose contents have changed since the file view was mapped. The FlushViewOfFile function does not flush the file metadata, and it does not wait to return until the changes are flushed from the underlying hardware disk cache and physically written to disk. To flush all the dirty pages plus the metadata for the file and ensure that they are physically written to disk, call FlushViewOfFile and then call the FlushFileBuffers function.
When flushing a memory-mapped file over a network, FlushViewOfFile guarantees that the data has been written from the local computer, but not that the data resides on the remote computer. The server can cache the data on the remote side. Therefore, FlushViewOfFile can return before the data has been physically written to disk.
When modifying a file through a mapped view, the last modification timestamp may not be updated automatically. If required, the caller should use SetFileTime to set the timestamp.
In Windows ServerВ 2012, this function is supported by the following technologies.
Technology | Supported |
---|---|
Server Message Block (SMB) 3.0 protocol | Yes |
SMB 3.0 Transparent Failover (TFO) | Yes |
SMB 3.0 with Scale-out File Shares (SO) | Yes |
Cluster Shared Volume File System (CsvFS) | Yes |
Resilient File System (ReFS) | Yes |
В
When CsvFs is paused this call might fail with an error indicating that there is a lock conflict.
File Stream. Flush Метод
Определение
Очищает буферы для этого потока и вызывает запись всех буферизованных данных в файл. Clears buffers for this stream and causes any buffered data to be written to the file.
Перегрузки
Очищает буферы для этого потока и вызывает запись всех буферизованных данных в файл. Clears buffers for this stream and causes any buffered data to be written to the file.
Очищает буферы для этого потока и вызывает запись всех буферизованных данных в файл, а также очищает все буферы промежуточных файлов. Clears buffers for this stream and causes any buffered data to be written to the file, and also clears all intermediate file buffers.
Flush()
Очищает буферы для этого потока и вызывает запись всех буферизованных данных в файл. Clears buffers for this stream and causes any buffered data to be written to the file.
Исключения
Ошибка ввода/вывода. An I/O error occurred.
Поток закрыт. The stream is closed.
Примеры
Этот пример кода является частью большого примера, приведенного для Lock метода. This code example is part of a larger example provided for the Lock method.
Комментарии
Этот метод переопределяет метод Stream.Flush. This method overrides Stream.Flush.
При вызове FileStream.Flush метода буфер ввода-вывода операционной системы также очищается. When you call the FileStream.Flush method, the operating system I/O buffer is also flushed.
Кодировщик потока не очищается, если не был явно вызван Flush или удален объект. A stream’s encoder is not flushed unless you explicitly call Flush or dispose of the object. Значение StreamWriter.AutoFlush true указывает, что данные будут сброшены из буфера в поток, но состояние кодировщика не будет сбрасываться. Setting StreamWriter.AutoFlush to true means that data will be flushed from the buffer to the stream, but the encoder state will not be flushed. Это позволяет кодировщику оставаться в состоянии (частичные символы), чтобы он мог правильно кодировать следующий блок символов. This allows the encoder to keep its state (partial characters) so that it can encode the next block of characters correctly. Этот сценарий влияет на UTF8 и UTF7, где определенные символы могут быть закодированы только после того, как кодировщик получит смежный символ или символы. This scenario affects UTF8 and UTF7 where certain characters can only be encoded after the encoder receives the adjacent character or characters.
Поскольку буфер можно использовать для чтения или записи, Flush() выполняет следующие две функции: Because a buffer can be used for either reading or writing, Flush() performs the following two functions:
Все данные, ранее записанные в буфер, копируются в файл, и буфер удаляется, за исключением состояния кодировщика. Any data previously written to the buffer is copied to the file and the buffer is cleared except for its encoder state.
Если BufferedStream.CanSeek параметр имеет значение true и данные были ранее скопированы из файла в буфер для чтения, текущее расположение в файле уменьшается на число непрочитанных байтов в буфере. If BufferedStream.CanSeek is true and data was previously copied from the file to the buffer for reading, the current position within the file is decremented by the number of unread bytes in the buffer. Затем буфер удаляется. The buffer is then cleared.
Используйте Flush(Boolean) перегрузку метода, если нужно убедиться, что все буферизованные данные в промежуточных файловых буферах записываются на диск. Use the Flush(Boolean) method overload when you want to ensure that all buffered data in intermediate file buffers is written to disk.
What exactly is Python’s file.flush() doing?
flush() does not necessarily write the file’s data to disk. Use flush() followed by os.fsync() to ensure this behavior.
So my question is: what exactly is Python’s flush doing? I thought that it forces to write data to the disk, but now I see that it doesn’t. Why?
4 Answers 4
There’s typically two levels of buffering involved:
- Internal buffers
- Operating system buffers
The internal buffers are buffers created by the runtime/library/language that you’re programming against and is meant to speed things up by avoiding system calls for every write. Instead, when you write to a file object, you write into its buffer, and whenever the buffer fills up, the data is written to the actual file using system calls.
However, due to the operating system buffers, this might not mean that the data is written to disk. It may just mean that the data is copied from the buffers maintained by your runtime into the buffers maintained by the operating system.
If you write something, and it ends up in the buffer (only), and the power is cut to your machine, that data is not on disk when the machine turns off.
So, in order to help with that you have the flush and fsync methods, on their respective objects.
The first, flush , will simply write out any data that lingers in a program buffer to the actual file. Typically this means that the data will be copied from the program buffer to the operating system buffer.
Specifically what this means is that if another process has that same file open for reading, it will be able to access the data you just flushed to the file. However, it does not necessarily mean it has been «permanently» stored on disk.
To do that, you need to call the os.fsync method which ensures all operating system buffers are synchronized with the storage devices they’re for, in other words, that method will copy data from the operating system buffers to the disk.
Typically you don’t need to bother with either method, but if you’re in a scenario where paranoia about what actually ends up on disk is a good thing, you should make both calls as instructed.
Addendum in 2018.
Note that disks with cache mechanisms is now much more common than back in 2013, so now there are even more levels of caching and buffers involved. I assume these buffers will be handled by the sync/flush calls as well, but I don’t really know.
How to flush all file buffers in Windows?
There is FlushFileBuffers() API in Windows to flush buffers till hard drive for a single file. There is sync() API in Linux to flush file buffers for all files.
However, is there WinAPI for flushing all files too, i.e. a sync() analog?
2 Answers 2
It is possible to flush the entire hard drive.
To flush all open files on a volume, call FlushFileBuffers with a handle to the volume. The caller must have administrative privileges. For more information, see Running with Special Privileges.
Also, the same article states the correct procedure to follow if, for some reason, data must be flushed: CreateFile function with the FILE_FLAG_NO_BUFFERING and FILE_FLAG_WRITE_THROUGH flags.
Due to disk caching interactions within the system, the FlushFileBuffers function can be inefficient when used after every write to a disk drive device when many writes are being performed separately. If an application is performing multiple writes to disk and also needs to ensure critical data is written to persistent media, the application should use unbuffered I/O instead of frequently calling FlushFileBuffers. To open a file for unbuffered I/O, call the CreateFile function with the FILE_FLAG_NO_BUFFERING and FILE_FLAG_WRITE_THROUGH flags. This prevents the file contents from being cached and flushes the metadata to disk with each write. For more information, see CreateFile.
But also check the restrictions of file buffering about memory and data alignment.