Using select / poll / kqueue / kevent to view the directory for new files

In my application, I need to look at the directory for new files. The volume of traffic is very large, and at least hundreds of new files per second will appear. I am currently using a busy cycle with this idea:

while True:
  time.sleep(0.2)
  if len(os.listdir('.')) > 0:
    # do stuff

After completing the profiling, I see a lot of time spent in a dream, and I wonder if I should change this to use the survey instead.

I'm trying to use one of the available classes in selectto poll my directory, but I'm not sure if it really works, or if I'm just doing it wrong.

I get fd for my directory with:

fd = os.open('.', os.O_DIRECT)

Then I tried several methods to see when the directory changes. For example, one of my attempts:

poll = select.poll()
poll.register(fd, select.POLLIN)

poll.poll()  # returns (fd, 1) meaning 'ready to read'

os.read(fd, 4096) # prints largely gibberish but i can see that i'm pulling the files/folders contained in the directory at least

poll.poll()  # returns (fd, 1) again

os.read(fd, 4096) # empty string - no more data

poll() , ? , , - .

?

, while True: look for changes?

+5
5

, , , , .

, , . , sleep, . , .

, , , , , .

+1

FreeBSD , , Mac OS X inotify, kqueue. man 2 kqueue FreeBSD . kqueue Freebsd PyKQueue, http://people.freebsd.org/~dwhite/PyKQueue/, , , .

+6

Python , gamin inotify ( pyinotify, ...) - , C , ...

+3

, select.kqueue - , kqueue - BSD , / ,

0
0

All Articles