本文介绍了Python龙卷风Web服务器:如何使用多重处理来加快Web应用程序的速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web应用程序,当一个用户使用它时运行良好,但是随着越来越多的客户端开始使用它,它的运行速度实在令人难以忍受.服务器端用python编写,并使用龙卷风.我注意到,虽然它在运行的服务器上有4个内核,但仅使用了1个内核,因此我开始研究python的多处理.我从以下位置看到了基本示例: http://sebastianraschka.com/Articles/2014_multiprocessing_intro.html和龙卷风处理器来自: http://tornado.readthedocs.org/en/latest/_modules/tornado /process.html (这似乎有点复杂),但是我仍然不确定这是我想要的东西.如果说有50个用户一次查看该代码,是否可以在4个处理器上运行该代码来加快速度?如果是的话,这些选项之一是在使用龙卷风Web服务器时实现的方式吗?

I have a web application that runs fine when used by one user, but as more clients start using it, it is unbearably slow. The server side is written in python and uses tornado. I noticed that while the server it's running on has 4 cores, only 1 is being used, so I've started looking at python's multiprocessing. I've seen the basic example from: http://sebastianraschka.com/Articles/2014_multiprocessing_intro.html and the tornado processors from:http://tornado.readthedocs.org/en/latest/_modules/tornado/process.html (which seems a bit more complicated), but I'm still not sure this is what I'm looking for. Will running this code with 4 processors speed it up when say 50 users are viewing it at once? And if so are one of these options the way to go for implementation when using tornado web server?

对于这个含糊不清的问题,我们深表歉意-即使经过大量研究,我对多处理的缺乏经验也应归咎于此.如果有任何示例代码可以帮助您回答此问题,请告诉我.

Sorry for the vague and poorly written question - my lack of experience with multiprocessing is to blame for this, even after extensive research. If any example code will help with answering this question, please let me know.

谢谢!

推荐答案

您可以使用以下代码运行多个龙卷风工人:

You can run multiple tornado workers with code like this:

import tornado.web
import tornado.httpserver
import tornado.ioloop

class MyHandler(tornado.web.RequestHandler):

    @tornado.web.asynchronous
    def get(self):
        self.write('OK')
        self.finish()

if __name__=='__main__':
    app = tornado.web.Application([(r'/', MyHandler)])

    server = tornado.httpserver.HTTPServer(app)
    server.bind(8888)

    server.start(4) # Specify number of subprocesses

    tornado.ioloop.IOLoop.current().start()

尽管很奇怪,您的应用程序即使在一个内核上也无法为50个用户提供服务.您在那里有大量计算,还是使用任何阻塞库?

Although it's pretty strange that your app have problems serving 50 users even on one core. Do you have some heavy computations there, or do you use any blocking libs?

确保在处理程序方法上使用@tornado.web.asynchronous@tornado.gen.coroutine装饰器,否则,您将同步运行代码.

Make sure you use @tornado.web.asynchronous or @tornado.gen.coroutine decorators on your handler methods, otherwise you are just running your code synchronously.

更新:为了在单独的过程中完成繁重的工作,可以使用concurrent.futures.ProcessPoolExecutor.看看这个答案: https://stackoverflow.com/a/25208213/1525432 .

UPDATE: In order to do the heavy-lifting work in a separate process you can use concurrent.futures.ProcessPoolExecutor. Take a look at this answer: https://stackoverflow.com/a/25208213/1525432.

这篇关于Python龙卷风Web服务器:如何使用多重处理来加快Web应用程序的速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 04:41