<br><br><div class="gmail_quote">On Sat, Mar 12, 2011 at 3:32 AM, Tim Allen <span dir="ltr">&lt;<a href="mailto:screwtape@froup.com">screwtape@froup.com</a>&gt;</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>
&gt; So far I have a &quot;working&quot; implementation ... but I&#39;m noticing that if I do<br>
&gt; the following:<br>
&gt; -- log when a message is received<br>
&gt; -- that for that message it &quot;might&quot; show up in the file a pretty lengthy<br>
&gt; 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&#39;t<br>
call flush(), Python (well, the C standard library) won&#39;t send the data<br>
on to the OS until its buffer is full, or the file handle is closed. If<br>
you&#39;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&#39;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&#39;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>
&gt; The actual UDP protocol:<br>
&gt;<br>
&gt; class VocsLogger(DatagramProtocol):<br>
&gt;     def datagramReceived(self, data, (host, port)):<br>
&gt;         _proc_msg(self.transport, data, (host,<br>
&gt; port))._new_msg().addCallback(handler)<br>
<br>
</div>_proc_msg doesn&#39;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>
&gt; The _proc_msg class:<br>
&gt;<br>
&gt; class _proc_msg:<br>
&gt;     def __init__(self, sck, data, (host, port)):<br>
&gt;         self._sck = sck<br>
&gt;         self._data = data<br>
&gt;         self._host = host<br>
&gt;         self._port = port<br>
&gt;<br>
&gt;     def _new_msg(self):<br>
&gt;         d, _ = LogMsg().ParseSocketMsg(self._data)<br>
&gt;         if d.type.upper() == DISKINFO[0]:<br>
&gt;             DISKINFO[1].write(d.ToString() + &quot;\n%s\n&quot; % (LOG_DELIM))<br>
&gt;         elif d.type.upper() == LOADAVG[0]:<br>
&gt;             LOADAVG[1].write(d.ToString() + &quot;\n%s\n&quot; % (LOG_DELIM))<br>
&gt;         elif d.type.upper() == MEMINFO[0]:<br>
&gt;             MEMINFO[1].write(d.ToString() + &quot;\n%s\n&quot; % (LOG_DELIM))<br>
&gt;         elif d.type.upper() == NETDEV[0]:<br>
&gt;             NETDEV[1].write(d.ToString() + &quot;\n%s\n&quot; % (LOG_DELIM))<br>
&gt;         elif d.type.upper() == PSAUX[0]:<br>
&gt;             PSAUX[1].write(d.ToString() + &quot;\n%s\n&quot; % (LOG_DELIM))<br>
&gt;         elif d.type.upper() == WHOINFO[0]:<br>
&gt;             WHOINFO[1].write(d.ToString() + &quot;\n%s\n&quot; % (LOG_DELIM))<br>
&gt;         else:<br>
&gt;             DEFAULT[1].write(d.ToString() + &quot;\n%s\n&quot; % (LOG_DELIM))<br>
<br>
</div>It depends on what DISKINFO[0] and DISKINFO[1] actually are, but<br>
assuming they&#39;re a string and a file-handle, this code would look more<br>
Pythonic as something like this:<br>
<br>
LOGSINKS = {<br>
        &quot;DISKINFO&quot;: open(&quot;/var/log/diskinfo&quot;, &quot;w&quot;),<br>
        &quot;LOADAVG&quot;: open(&quot;/var/log/loadavg&quot;, &quot;w&quot;),<br>
        &quot;MEMINFO&quot;: open(&quot;/var/log/meminfo&quot;, &quot;w&quot;),<br>
        &quot;NETDEV&quot;: open(&quot;/var/log/netdev&quot;, &quot;w&quot;),<br>
        &quot;PSAUX&quot;: open(&quot;/var/log/psaux&quot;, &quot;w&quot;),<br>
        &quot;WHOINFO&quot;: open(&quot;/var/log/whoinfo&quot;, &quot;w&quot;),<br>
        &quot;DEFAULT&quot;: open(&quot;/var/log/default&quot;, &quot;w&quot;),<br>
    }<br>
<br>
def _new_msg(self, data):<br>
    d, _ = LogMsg().ParseSocketMsg(data)<br>
    type = d.type.upper()<br>
    sink = LOGSINKS.get(type, LOGSINKS[&#39;DEFAULT&#39;])<br>
    sink.write(&quot;%s\n%s\n&quot; % (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>