0

Have a simple webserver in python:

try:
    server = HTTPServer(('', 80), MyHandler)
    print 'started httpserver...'
    server.serve_forever()
except KeyboardInterrupt:
    print '^C received, shutting down server'
    server.socket.close()

Is there any way I can make it faster? I believe the above is blocking so concerned about slow responses...

Thanks!

Constantinius
  • 34,183
  • 8
  • 77
  • 85
Kid_A
  • 47
  • 4
  • Yes. There is a way to make it faster and non-blocking. Perhaps you should modify your question slightly to emphasize what you're trying to accomplish and why you started with this as your first effort. – S.Lott Aug 31 '11 at 12:43
  • 1
    For nonblocking sockets please take a look at twisted or tornado. It's the fastest web servers on python as I know. But they may be pretty complicated. You also may use ForkingMixin or ThreadingMixin for HTTP server to prevent blocking between requests. – varela Aug 31 '11 at 12:48
  • Thanks much to both of you. I'm running a simple user study for which I designed a browser plugin. I have a server component that the plugin communicates with periodically. I'm noticing the response times at the client (; the plugin) are sometimes slow. I want to speed up the web server -- wanted to know how without making it too complicated. – Kid_A Aug 31 '11 at 12:55

1 Answers1

1

To make web server scaleble use non-blocking IO sockets. A good framework/server for Python is Spawning:

http://pypi.python.org/pypi/Spawning/

Note that this makes your service scalable only in horizontally (you can easily add more concurrent requests). The delay of processing a single request depends on your code (how you make database connections, etc.) and hardware (do not use shared hosting). You did not explain in detail in your question what you are processing, so there is not enough data to give a comprehensive answer to your question.

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435