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 8605977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:58:54+00:00 2026-06-12T02:58:54+00:00

I’ve set up a threaded (with Python threads) HTTP server by creating a class

  • 0

I’ve set up a threaded (with Python threads) HTTP server by creating a class that inherits from HTTPServer and ThreadingMixIn:

class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
    pass

I have a handler class which inherits from BaseHTTPRequestHandler, and I start the server with something like this:

class MyHandler(BaseHTTPRequestHandler):
    ...

server = ThreadedHTTPServer(('localhost', 8080), MyHandler)
# Prevent issues with socket reuse
server.allow_reuse_address = True
# Start the server
server.serve_forever()

This is all pretty straightforward. The problem that I’m encountering is that, ThreadingMixIn, ForkingMixIn, or otherwise, the request winds up blocking on the request handler to return. This can easily be seen by implementing this example code:

class MyHandler(BaseHTTPRequestHandler):
    def respond(self, status_code):
        self.send_response(status_code)
        self.end_headers()

    def do_GET(self):
         print "Entered GET request handler"
         time.sleep(10)
         print "Sending response!"
         respond(200)

If the server were processing these simultaneously, then we would be able to send two requests and see the server enter both GET request handlers before sending either response. Instead, the server will enter the GET request handler for the first request, wait for it to return, then enter it for the second (so the second request takes ~20 seconds to return instead of 10).

Is there a straightforward way for me to implement a system where the server doesn’t wait on the handler to return? Specifically, I’m trying to write a system which waits to receive several requests before returning any of them (a form of long polling) and running into issues where the first request waiting blocks any future requests from connecting to the server.

  • 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-06-12T02:58:55+00:00Added an answer on June 12, 2026 at 2:58 am
    class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
        pass
    

    is enough. Your client probably don’t make concurrent requests. If you make the requests in parallel the threaded server works as expected. Here’s the client:

    #!/usr/bin/env python
    import sys
    import urllib2
    
    from threading import Thread
    
    def make_request(url):
        print urllib2.urlopen(url).read()
    
    def main():
        port = int(sys.argv[1]) if len(sys.argv) > 1 else 8000
        for _ in range(10):
            Thread(target=make_request, args=("http://localhost:%d" % port,)).start()
    
    main()
    

    And the corresponding server:

    import time
    from BaseHTTPServer   import BaseHTTPRequestHandler, HTTPServer, test as _test
    from SocketServer     import ThreadingMixIn
    
    
    class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
        pass
    
    class SlowHandler(BaseHTTPRequestHandler):
        def do_GET(self):
            self.send_response(200)
            self.send_header("Content-type", "text/plain")
            self.end_headers()
    
            self.wfile.write("Entered GET request handler")
            time.sleep(1)
            self.wfile.write("Sending response!")
    
    def test(HandlerClass = SlowHandler,
             ServerClass = ThreadedHTTPServer):
        _test(HandlerClass, ServerClass)
    
    
    if __name__ == '__main__':
        test()
    

    All 10 requests finish in 1 second. If you remove ThreadingMixIn from the server definition then all 10 requests take 10 seconds to complete.

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

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

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.