poll of filesystem

B

Belisko Marek

Hi,

just want to use poll method to get data from /proc file system. Use
simple code for it. Problem is it seems poll return POLLIN flag but
when I try to read data it's always empty. Could be a problem changes
are so fast that print can't print it?

Code:

#!/usr/bin/python

import select
from select import POLLIN, POLLERR

p = select.poll()

fd = open("/proc/loadavg", "r")

p.register(fd.fileno(), POLLIN)

while True:
events = p.poll(1000)
if (events == -1):
print "timeout"
fd0, event = events[0]
if (event == POLLIN):
print fd.read()

Thanks,

marek
--
as simple and primitive as possible
-------------------------------------------------
Marek Belisko - OPEN-NANDRA
Freelance Developer

Ruska Nova Ves 219 | Presov, 08005 Slovak Republic
Tel: +421 915 052 184
skype: marekwhite
icq: 290551086
web: http://open-nandra.com
 
N

Nobody

just want to use poll method to get data from /proc file system. Use
simple code for it. Problem is it seems poll return POLLIN flag but
when I try to read data it's always empty. Could be a problem changes
are so fast that print can't print it?

poll() doesn't work for files; they are always readable. It's meant for
pipes, sockets and character devices (ttys, etc). If you're looking for a
way to find out when a file has changed, there isn't one (Linux has
inotify, but that isn't portable and it doesn't work with /proc).

The reason why fd.read() doesn't return any data after the first call is
that you're already at EOF; you need to reset the file position with
fd.seek(0) to read more data (however: for /proc, I would recommend
closing the file and reopening it).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top