开发者

Multi threaded webserver vs single threaded

We have a simple webserver for internal use that has only one duty: listen for requests, read them, and push the data in a database. The db and webserver are both located on the same machine. The db is a mysql-db and the server is a python webserver (BaseHTTPServer.HTTPServer) which runs single threaded.

The problem is that two requests can't be handled at the same time. The question is, would it help to make the webserver multi-threaded (using django, cheryypy,..)? Intuitively, the webserver is only performing CPU 开发者_运维问答consuming tasks so changing it to multi-threaded shouldn't help. Is this correct?


Having several threads or processes will indeed help you (indeed it's in practice required) when you want to handle more than one request at a time.

That doesn't mean that the two requests will be handled faster. Having a pool of processes or threads is very helpful for webserver performance, but that's not really noticeable in cases like this (unless you have multiple cores). But MySQL has no problem handling two requests at the same time, so if your webserver can do it as well, then you get rid of the problem of handling just one request.

But if it's worth the effort to start using such a server only you can answer. :) Django is surely overkill in any case, look at some small WSGI server.


Step 1. Use Apache. It may seem like overkill but it's cheap multi-threading with no programming on your part. Apache can be configured to fork off several servers. No programming on your part.

This may be sufficient to make your application run concurrently.

Step 2. Rewrite your application to use the wsgi framework and embed it in the wsgiref server. This won't change much, but it's the way you should always write small web applications.

Step 3. Use mod_wsgi in Apache. It allows you to have one or more background daemon versions of your application. With no additional programming you get a heap of concurrency available to you.

Important lesson learned. Always use WSGI.


My instinct says you're right, but it also says that using Django or Cherrypy is WAY overkill, even if you did want to make it multithreaded.

I think you're right, though, since if the webserver really isn't doing anything other than working a DB, then any other thread wouldn't be able to do anything other than start a response. The client will happily wait the ten-to-hundred ms necessary for the other request to finish, and then the server will be able to accept() right away.


If the Web server is single-threaded and DB requests are synchronous (meaning the Web server is blocked while the DB request is being processed) then making it multi-threaded would help.

This would enable you to process multiple requests simultaneously. Your DB engine is probably quite good at that. However, right now you're not letting it to service multiple requests simultaneously because you have the Web server sitting right in front of it that only feeds it one request at a time.


You should go for multithreaded or asynchronous webserver, otherwise each request is being blocked. Django is a web framework, you might have to look for scripts which you can transparently replace in your current setup and still have your pure python multithreaded webserver. Otherwise twisted is a good solution too. AFA I can see, you may not want a Web Framework because you are not doing a template driven MVC style app.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜