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

  • Home
  • SEARCH
  • 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 498239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:51:33+00:00 2026-05-13T05:51:33+00:00

I am writing a cgi page in Python. Let’s say a client sends request

  • 0

I am writing a cgi page in Python. Let’s say a client sends request to my cgi page. My cgi page does the calculation and as soon as it has the first output, it sends back that output to the client, but it will CONTINUE to do the calculation and send other responses AFTER the first response is sent.

Is what I have presented here possible? I ask this question because in my limited knowledge, in a cgi page responses are sent back on one-time basic, once a response is sent, cgi-page stops running. This thing is made on server side or client side, and how do I implement it?

My server is running Apache. Thank you very much.

I have tried a client code from “dbr” in this forum (thanks to him I got the idea of how long-polling works).

<html>
<head>
    <title>BargePoller</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type="text/javascript" charset="utf-8"></script>

    <style type="text/css" media="screen">
      body{ background:#000;color:#fff;font-size:.9em; }
      .msg{ background:#aaa;padding:.2em; border-bottom:1px #000 solid}
      .old{ background-color:#246499;}
      .new{ background-color:#3B9957;}
    .error{ background-color:#992E36;}
    </style>

    <script type="text/javascript" charset="utf-8">
    function addmsg(type, msg){
        /* Simple helper to add a div.
        type is the name of a CSS class (old/new/error).
        msg is the contents of the div */
        $("#messages").append(
            "<div class='msg "+ type +"'>"+ msg +"</div>"
        );
    }

    function waitForMsg(){
        /* This requests the url "msgsrv.php"
        When it complete (or errors)*/
        $.ajax({
            type: "GET",
            url: "msgsrv.php",

            async: true, /* If set to non-async, browser shows page as "Loading.."*/
            cache: false,
            timeout:50000, /* Timeout in ms */

            success: function(data){ /* called when request to barge.php completes */
                addmsg("new", data); /* Add response to a .msg div (with the "new" class)*/
                setTimeout(
                    'waitForMsg()', /* Request next message */
                    1000 /* ..after 1 seconds */
                );
            },
            error: function(XMLHttpRequest, textStatus, errorThrown){
                addmsg("error", textStatus + " (" + errorThrown + ")");
                setTimeout(
                    'waitForMsg()', /* Try again after.. */
                    "15000"); /* milliseconds (15seconds) */
            },
        });
    };

    $(document).ready(function(){
        waitForMsg(); /* Start the inital request */
    });
    </script>
</head>
<body>
    <div id="messages">
        <div class="msg old">
            BargePoll message requester!
        </div>
    </div>
</body>
</html>

And here is my server code:

import sys
if __name__ == "__main__":
    sys.stdout.write("Content-Type: text/html\r\n\r\n")
    print "<html><body>"
    for i in range(10):
        print "<div>%s</div>" % i
        sys.stdout.flush()
    print "</body></html>"

I am expecting my client page to display 1 number at a time (0,1,2,…), but the data always comes out all at once (01234…). Please help me figure it out. Thanks you guys so much.

Just a little out-track, I am trying to use jquery comet plugin, but I couldn’t find sufficient documentation though. Helps would be much appreciated. Thanks again 😀

[edit] Ok guys, finally thanks to your guides I have managed to make it work. You’re right when predict that mod_deflate is the source of all this.

To sum up, what I have done here:

  • For client, make a long poll page as the html code above

  • For server, disable the mod_deflate by: editing file /etc/apache2/mods-available/deflate.conf, comment out the line with text/html part and restart the server. To ensure that Python doesn’t buffer the output itself, include #!/usr/bin/python -u in the beginning of the page. Remember to use sys.stdout.flush() after each printing that you want to appear at the client. The effect may not be transparent, should include time.sleep(1) to test. 😀

Thanks you guys very much for supporting and helping solving this 😀

  • 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-13T05:51:34+00:00Added an answer on May 13, 2026 at 5:51 am

    Sure.

    There’s traditional server-driven approach, where the script runs just once, but takes a long time to complete, spitting out bits of page as it goes:

    import sys, time
    
    sys.stdout.write('Content-Type: text/html;charset=utf-8\r\n\r\n')
    
    print '<html><body>'
    for i in range(10):
        print '<div>%i</div>'%i
        sys.stdout.flush()
        time.sleep(1)
    

    When writing an app to WSGI, this is done by having the application return an iterable which outputs each block it wants sent separately one at a time. I’d really recommend writing to WSGI; you can deploy it through CGI now, but in the future when your app needs better performance you can deploy it through a faster server/interface without having to rewrite.

    WSGI-over-CGI example:

    import time, wsgiref.handlers
    
    class MyApplication(object):
        def __call__(self, environ, start_response):
            start_response('200 OK', [('Content-Type', 'text/html;charset=utf-8')])
            return self.page()
    
        def page(self):
            yield '<html><body>'
            for i in range(10):
                yield '<div>%i</div>'%i
                time.sleep(1)
    
    application= MyApplication()
    if __name__=='__main__':
        wsgiref.handlers.CGIHandler().run(application)
    

    Note that your web server may foil this approach (for CGI or WSGI) by adding buffering of its own. This typically happens if you’re using output-transforming filters like mod_deflate to automatically compress webapp output. You’ll need to turn compression off for partial-response-generating scripts.

    This limits you to rendering the page bit-by-bit as new data comes in. You can make it prettier by having the client-side take care of altering the page as new data comes in, eg.:

    def page(self):
        yield (
            '<html><body><div id="counter">-</div>'
            '<script type="text/javascript">'
            '    function update(n) {'
            '        document.getElementById("counter").firstChild.data= n;'
            '    }'
            '</script>'
        )
        for i in range(10):
            yield '<script type="text/javascript">update(%i);</script>'%i
            time.sleep(1)
    

    This relies on client-side scripting so it might be a good idea to include backup non-script-based final output at the end.

    All the while doing this, the page will appear to be still loading. If you don’t want that, then you’d need to split the script into a first request that just spits out the static content, including client-side script that checks back with the server using either one XMLHttpRequest that it polls for new data through, or, for the really long-running cases, many XMLHttpRequests each of which returns the status and any new data. This approach is much more complicated as it means you have to run your work process as a background daemon process apart from the web server, and pass data between the daemon and the front-end CGI/WSGI request using eg. pipes or a database.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 364k
  • Answers 364k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The view model is a class that represents the fields… May 14, 2026 at 3:37 pm
  • Editorial Team
    Editorial Team added an answer I ended up asking my question a different way, and… May 14, 2026 at 3:37 pm
  • Editorial Team
    Editorial Team added an answer NSString *toggleValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"enabled_preference"]; NSArray *imagesA = [NSArray… May 14, 2026 at 3:37 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.