Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #8660 > unrolled thread
| Started by | Belisko Marek <marek.belisko@gmail.com> |
|---|---|
| First post | 2011-07-02 11:40 +0200 |
| Last post | 2011-07-02 15:37 -0700 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.python
poll of filesystem Belisko Marek <marek.belisko@gmail.com> - 2011-07-02 11:40 +0200
Re: poll of filesystem Nobody <nobody@nowhere.com> - 2011-07-02 18:49 +0100
Re: poll of filesystem Tim Roberts <timr@probo.com> - 2011-07-02 15:37 -0700
| From | Belisko Marek <marek.belisko@gmail.com> |
|---|---|
| Date | 2011-07-02 11:40 +0200 |
| Subject | poll of filesystem |
| Message-ID | <mailman.557.1309599650.1164.python-list@python.org> |
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
[toc] | [next] | [standalone]
| From | Nobody <nobody@nowhere.com> |
|---|---|
| Date | 2011-07-02 18:49 +0100 |
| Message-ID | <pan.2011.07.02.17.49.17.495000@nowhere.com> |
| In reply to | #8660 |
On Sat, 02 Jul 2011 11:40:46 +0200, Belisko Marek wrote: > 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).
[toc] | [prev] | [next] | [standalone]
| From | Tim Roberts <timr@probo.com> |
|---|---|
| Date | 2011-07-02 15:37 -0700 |
| Message-ID | <487v0755lagsc7okn7fesp9f5l8ssvdch3@4ax.com> |
| In reply to | #8660 |
Belisko Marek <marek.belisko@gmail.com> wrote:
>
>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 make sense here. The data in /proc/loadavg data is not text
data that gets updated periodically. There is no file on disk somewhere
called /proc/loadavg. Instead, reading /proc/loadavg causes a call into a
kernel driver, and reads data directly from variables stored in the kernel.
The data is ALWAYS available.
If you want the data once a second, just do it the easy way:
import time
while True:
print open('/proc/loadavg').read()
time.sleep(1)
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web