osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Understand workflow about reading and writing files in Python


I'm trying to understand the workflow of how Python read/writes data with
buffer. I will be appreciated if someone can review it.

### Read n data
1. If the data already in the buffer, return data
2. If the data not in the buffer:
    1. copy all the current data from the buffer
    2. create a new buffer object, fill the new buffer with raw read which
read data from disk.
    3. concat the data in the old buffer and new buffer.
    4. return the data

### Write n data
1. If data small enough to fill into the buffer, write data to the buffer
2. If data can't fill into the buffer
    1. flush the data in the buffer
        1. If succeed:
            1. create a new buffer object.
            2. fill the new buffer with data return from raw write
        2. If failed:
            1. Shifting the buffer to make room for writing data to the
buffer
            2. Buffer as much writing data as possible (may raise
BlockingIOError)
    2. return the data