2

Does anybody know if select.select() works with regular files or just with sockets/pipes?

I've tried on Solaris, Linux and Mac OS X - it doesn't block on select.select() call.

It just explodes my brain, trying something like this with no luck

import os
import select

fds = [ os.open("read.txt", os.O_RDONLY) ]

while True:
    reads, _, _ = select.select(fds, [], [], 2.0)
    if 0 < len(reads):
        print "-> ",os.read(reads[0], 10)
    else:
        print "timeout"
Taras
  • 910
  • 7
  • 13
  • 1
    This has less to do with python than with the underlying operating system. Better read up on non-blocking io and asynchronous io with regular files. – ʇsәɹoɈ Jan 31 '11 at 18:26

2 Answers2

2

From the documentation:

Note that on Windows, it only works for sockets; on other operating systems, it also works for other file types (in particular, on Unix, it works on pipes). It cannot be used on regular files to determine whether a file has grown since it was last read.

Does that help?

mtrw
  • 34,200
  • 7
  • 63
  • 71
1

select should work for files also, but I think FD for files gonna be always ready.

You should also check if you reached the end of the file. Here is an example which works for me:

import os
import select

fds = [ os.open("data", os.O_RDONLY) ]

while True:
    reads, _, _ = select.select(fds, [], [], 2.0)
    if 0 < len(reads):
        d = os.read(reads[0], 10)
        if d:
            print "-> ", d
        else:
            break
    else:
        print "timeout"
Elalfer
  • 5,312
  • 20
  • 25