<br><br><div class="gmail_quote">On Sat, Mar 12, 2011 at 3:32 AM, Tim Allen <span dir="ltr"><<a href="mailto:screwtape@froup.com">screwtape@froup.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div class="im">On Fri, Mar 11, 2011 at 01:15:47PM -0600, SIC FS LIST wrote:<br>
> So far I have a "working" implementation ... but I'm noticing that if I do<br>
> the following:<br>
> -- log when a message is received<br>
> -- that for that message it "might" show up in the file a pretty lengthy<br>
> period of time later<br>
<br>
</div>Assuming the objects stored in DISKINFO[1] etc. are file objects, you<br>
seem to be writing to the files but never calling flush(). If you don't<br>
call flush(), Python (well, the C standard library) won't send the data<br>
on to the OS until its buffer is full, or the file handle is closed. If<br>
you're not getting that many log lines, it can take a while for that to<br>
happen.<br>
<br>
Of course, if you flush after every disk read, your program will run<br>
a bit more slowly and with more I/O... for an application where<br>
reliability is more important than performance (like logging) that's<br>
probably acceptable.<br></blockquote><div><br>You may also setup a timer that flushes files every, say, 5 minutes.<br> <br>One other thing I've read in the Python.org site is that flush() is not 100% sure to work immediately and should be used in combination with os.fsync(). Is there someone that can explain if that is correct?<br>
<br></div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div class="im"><br>
> The actual UDP protocol:<br>
><br>
> class VocsLogger(DatagramProtocol):<br>
> def datagramReceived(self, data, (host, port)):<br>
> _proc_msg(self.transport, data, (host,<br>
> port))._new_msg().addCallback(handler)<br>
<br>
</div>_proc_msg doesn't seem to be complicated enough to need its own class,<br>
why not just do what _proc_msg does in VocsLogger?<br>
<div class="im"><br>
> The _proc_msg class:<br>
><br>
> class _proc_msg:<br>
> def __init__(self, sck, data, (host, port)):<br>
> self._sck = sck<br>
> self._data = data<br>
> self._host = host<br>
> self._port = port<br>
><br>
> def _new_msg(self):<br>
> d, _ = LogMsg().ParseSocketMsg(self._data)<br>
> if d.type.upper() == DISKINFO[0]:<br>
> DISKINFO[1].write(d.ToString() + "\n%s\n" % (LOG_DELIM))<br>
> elif d.type.upper() == LOADAVG[0]:<br>
> LOADAVG[1].write(d.ToString() + "\n%s\n" % (LOG_DELIM))<br>
> elif d.type.upper() == MEMINFO[0]:<br>
> MEMINFO[1].write(d.ToString() + "\n%s\n" % (LOG_DELIM))<br>
> elif d.type.upper() == NETDEV[0]:<br>
> NETDEV[1].write(d.ToString() + "\n%s\n" % (LOG_DELIM))<br>
> elif d.type.upper() == PSAUX[0]:<br>
> PSAUX[1].write(d.ToString() + "\n%s\n" % (LOG_DELIM))<br>
> elif d.type.upper() == WHOINFO[0]:<br>
> WHOINFO[1].write(d.ToString() + "\n%s\n" % (LOG_DELIM))<br>
> else:<br>
> DEFAULT[1].write(d.ToString() + "\n%s\n" % (LOG_DELIM))<br>
<br>
</div>It depends on what DISKINFO[0] and DISKINFO[1] actually are, but<br>
assuming they're a string and a file-handle, this code would look more<br>
Pythonic as something like this:<br>
<br>
LOGSINKS = {<br>
"DISKINFO": open("/var/log/diskinfo", "w"),<br>
"LOADAVG": open("/var/log/loadavg", "w"),<br>
"MEMINFO": open("/var/log/meminfo", "w"),<br>
"NETDEV": open("/var/log/netdev", "w"),<br>
"PSAUX": open("/var/log/psaux", "w"),<br>
"WHOINFO": open("/var/log/whoinfo", "w"),<br>
"DEFAULT": open("/var/log/default", "w"),<br>
}<br>
<br>
def _new_msg(self, data):<br>
d, _ = LogMsg().ParseSocketMsg(data)<br>
type = d.type.upper()<br>
sink = LOGSINKS.get(type, LOGSINKS['DEFAULT'])<br>
sink.write("%s\n%s\n" % (d.ToString(), LOG_DELIM))<br>
<br>
Hope that helps!<br>
<br>
_______________________________________________<br>
Twisted-Python mailing list<br>
<a href="mailto:Twisted-Python@twistedmatrix.com">Twisted-Python@twistedmatrix.com</a><br>
<a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python" target="_blank">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python</a><br>
</blockquote></div><br>