Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4070544
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:38:27+00:00 2026-05-20T16:38:27+00:00

I have a simple loop like: #Core Loop chunk_size=1000 while True: line_c = 0

  • 0

I have a simple loop like:

#Core Loop
chunk_size=1000
while True:
    line_c = 0 
    chunk_array = []
    while True:
        line = sys.stdin.readline()
        line_c +=1
        m = line_regex.match(line)
        if m:   
            chunk_array.append(m.groupdict())
        if line_c >= chunk_size:
            #print top_value(chunk_array, 'HTTP_HOST', 10)
            print stats(chunk_array, 'HTTP_HAPROXY_TT')
            break

The script is called as a unix filter, for example:

tail -f /var/log/web/stackoverflow.log | python logFilter.py

Instead of printing every X lines, what would be a good way to refactor this loop to do every X seconds?

Reference:
Stats function:

def stats(l, value):
    '''stats of an integer field'''
    m = []
    for line in l:
        if line[value].isdigit():
            m.append(int(line[value]))
    return "Mean: %s Min: %s Max: %s StdDev: %s" % (mean(m), amin(m), amax(m), std(m))

The input will be lines of a web log file, the line_regex turns them into field value pairs (groupdict). The output when using the stats function is like:

tail -f /var/log/web/stackoverflow.log | python logFilter.py -f HTTP_HAPROXY_TR -t stats
Mean: 183.43919598 Min: 0 Max: 3437 StdDev: 321.673112066
Mean: 182.768304915 Min: 0 Max: 2256 StdDev: 255.039386654
Mean: 142.672064777 Min: 0 Max: 1919 StdDev: 208.870675922

So those stat lines are printed every time the script has received 1000 lines. Instead of doing it every X number of lines, I would like to change the loop so this happens every say 10 seconds.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T16:38:28+00:00Added an answer on May 20, 2026 at 4:38 pm

    Do this

    import time
    def time_chunk( some_source, period=10 ):
        start= time.time()
        buffer= []
        for line in some_source:
            buffer.append( line )
            if time.time() - start >= period:
                start= time.time()
                yield buffer
                buffer= []
        yield buffer
    
    for chunk in time_chunk( sys.stdin ):  
        print( stats( chunk ) )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have simple regex \.*\ for me its says select everything between and ,
Ok, i have simple scenario: have two pages: login and welcome pages. im using
In general, is it a best practice to have simple POJO Java classes implement
I develop tools in Autodesk Maya. Many of the tools I build have simple
Should simple JavaBeans that have only simple getters and setters be unit tested?? What
I have a simple webform that will allow unauthenticated users to input their information,
I have a simple 2-column layout with a footer that clears both the right
I have a simple page with my ScriptManager and my UpdatePanel , and my
I have a simple little test app written in Flex 3 (MXML and some
I have a simple setter method for a property and null is not appropriate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.