使用turbogears2同时请求

时间:2017-03-11 09:06:27

标签: python ajax concurrency turbogears2

我是web dev的新手,我正在尝试构建一个简单的Web界面,使用Ajax调用来刷新数据,并使用turbogears2作为后端。

我的Ajax调用工作正常并定期调用我的Turbogears2服务器,但这些调用需要一些时间才能完成(某些请求会使服务器在其他计算机上使用远程SSH调用,这需要3-4秒才能完成)。

我的问题是TurboGears在处理下一个请求之前等待每个请求完成,所以我的所有并发Ajax调用都在排队,而不是全部并行处理。 要刷新N值需要3 * N秒,其中并发时间可能只需3秒。

知道如何解决这个问题吗?

这是我当前的服务器端代码(方法get_load是使用Ajax调用的方法):

class RootController(TGController):
@expose()
def index(self):
    with open ("index.html") as data:
        index = data.read()
    return index


@expose()
def get_load(self, ip):
    command = "bash get_cpu_load.sh"
    request = subprocess.Popen(["ssh", "-o ConnectTimeout=2", ip, command])
    load = str(request.communicate()[0])
    return load

1 个答案:

答案 0 :(得分:1)

您的问题可能是因为您使用 Gearbox wsgiref服务器提供请求。默认情况下,wsgiref服务器是单线程的,因此可以在一次服务单个请求。可以通过在wsgiref.threaded = true 服务器部分中提供development.ini配置选项来更改(同样也指定了IP地址和端口)。有关其他详细信息,请参阅https://github.com/TurboGears/gearbox#gearbox-http-servershttp://turbogears.readthedocs.io/en/latest/turbogears/gearbox.html#changing-http-server

请注意,wsgiref是TurboGears的开发服务器,通常不鼓励使用生产。在部署应用程序时,您应该考虑使用waitress,chaussette或mod_wsgi之类的东西,请参阅http://turbogears.readthedocs.io/en/latest/cookbook/deploy/index.html?highlight=deploy

相关问题