Created
November 26, 2012 10:17
-
-
Save syohex/4147516 to your computer and use it in GitHub Desktop.
Buffer flush sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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