Skip to content

Instantly share code, notes, and snippets.

@syohex
Created November 26, 2012 10:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save syohex/4147516 to your computer and use it in GitHub Desktop.
Save syohex/4147516 to your computer and use it in GitHub Desktop.
Buffer flush sample
#!perl
use strict;
use warnings;
use POSIX ();
sysopen my $in, "/proc/net/dev", POSIX::O_RDONLY or die "Can't open file: $!";
open my $out, '>', 'output.log' or die "Can't open file: $!";
#$out->autoflush(1); # <== バッファリングしない(autoflush $out, 1; も可)
my $interval = 3;
while (1) {
seek $in, 0, POSIX::SEEK_SET; # reset file offset
printf {$out} "%s", time();
while (my $buf = <$in>) {
if ($buf =~ /.+:.+/) {
chomp $buf;
$buf =~ s/.+:\s+//;
printf {$out} "\t%s", $buf;
}
}
printf {$out} "\n";
$out->flush(); # <== バッファの内容をフラッシュ
sleep $interval;
}
close $out;
close $in;
#!/usr/bin/env python
import re
import time
input = open("/proc/net/dev")
output = open("log.log", "w")
interval = 3
while True:
input.seek(0)
output.write( str(time.time()) )
for line in input.readlines():
if re.search(r'.+:.+', line):
replaced = re.sub(r'.+:\s+', '', line.rstrip('rn'))
output.write("\t%s" % replaced)
output.flush()
output.write("\n")
time.sleep(3)
close(output)
close(input)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment