Skip to content

Instantly share code, notes, and snippets.

@kwlzn
Created January 3, 2013 21:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kwlzn/4447291 to your computer and use it in GitHub Desktop.
Save kwlzn/4447291 to your computer and use it in GitHub Desktop.
quick/dirty stdin line counter in python
#!/usr/bin/env python
import sys, time, select, signal
try: window = int(sys.argv[1])
except: print 'usage: %s <window>' % sys.argv[0]; sys.exit(1)
a_time, b_time = long( time.time() ), 0
count = 0
def ctrlc(signum, frame): sys.exit(0)
signal.signal(signal.SIGINT, ctrlc)
while not sys.stdin.closed:
ready = select.select([sys.stdin], [], [], window)
if ready[0]:
l = sys.stdin.readline()
if not l: break
count += 1
b_time = long( time.time() )
if (b_time-a_time) >= window:
print count
sys.stdout.flush()
count, a_time = 0, b_time
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment