[Python-ideas] Atomic file.get(offset, length)

Cameron Simpson cs at zip.com.au
Mon Jul 23 01:04:30 CEST 2012


On 21Jul2012 13:35, Guido van Rossum <guido at python.org> wrote:
| On Sat, Jul 21, 2012 at 12:35 PM, Terry Reedy <tjreedy at udel.edu> wrote:
| > On 7/21/2012 2:59 PM, Matt Chaput wrote:
| >> I wish Python binary file objects had an atomic seek-read method, so
| >> I wouldn't have to perform my own locking everywhere to prevent other
| >> threads from moving the file pointer between seek and read.
[...]
| >> Is this something that can be bubbled up from the underlying
| >> platform? I think the Linux C equivalent is pread.
[...]
| "man pread" on OS/X suggests it exists there too. I presume the use
| case is to have a large data file open for reading by multiple
| threads. This is a reasonable use case and it makes some sense to
| extend our binary readable streams (buffered and unbuffered) with an
| API for this purpose.

On most Linux boxen you can say:

  man 3p pread

which will show you the POSIX man page, if it exists.

And it does!

So pread will exist on pretty much every UNIX platform, and I'd be
amazed if it wasn't on Windows.

In fact, it remarks that pread appeared in SysVr4, which is quite old.

| However, it's probably just efficient to just have a separate open
| stream per thread

It doubles the system call count per read (if pread is a system call,
which it ually will be (it is on Linux and MacOSX, and is hard to implement
otherwise without an annoying and slow locking scheme concealed inside
the C library).

I'd be +1 for adding pread and pwrite to the os module. It seems
reasonable and quite useful and should work on most platforms.

Cheers,
-- 
Cameron Simpson <cs at zip.com.au>

Rimmer: It will be happened; it shall be going to be happening; it will be
    was an event that could will have been taken place in the future.
        - Red Dwarf, _Future Echoes_



More information about the Python-ideas mailing list