简体   繁体   English

同时使用turbogears2请求

[英]Simultaneous requests with turbogears2

I'm very new to web dev, and i'm trying to build a simple Web interface with Ajax calls to refresh data, and turbogears2 as the backend. 我是Web开发人员的新手,我正在尝试构建一个简单的Web界面,并使用Ajax调用来刷新数据,并使用turbogears2作为后端。

My Ajax calls are working fine and makes periodic calls to my Turbogears2 server, however these calls takes time to complete (some requests make the server to use remote SSH calls on other machines, which takes up to 3-4 seconds to complete). 我的Ajax调用工作正常,并且可以定期调用我的Turbogears2服务器,但是这些调用需要一些时间才能完成(某些请求使该服务器在其他机器上使用远程SSH调用,这最多需要3-4秒才能完成)。

My problem is that TurboGears waits for each request to complete before handling the next one, so all my concurrent Ajax calls are being queued instead of being all processed in parallel. 我的问题是TurboGears在处理下一个请求之前等待每个请求完成,因此所有并发的Ajax调用都在排队,而不是并行处理。 To refresh N values takes 3*N seconds where it could just take 3 seconds with concurrency. 刷新N个值需要3 * N秒,而并发可能只需要3秒。

Any idea how to fix this ? 任何想法如何解决这个问题?

Here is my current server-side code (method get_load is the one called with Ajax): 这是我当前的服务器端代码(方法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

Your problem is probably caused by the fact that you are serving requests with Gearbox wsgiref server. 您的问题可能是由于您正在使用Gearbox wsgiref服务器处理请求而造成的。 By default the wsgiref server is single threaded and so can serve a single request at time. 默认情况下,wsgiref服务器是单线程的,因此可以同时处理单个请求。 That can be changed by providing the wsgiref.threaded = true configuration option in your development.ini server section (the same where ip address and port are specified too). 可以通过在您的development.ini 服务器部分中提供wsgiref.threaded = true配置选项(也可以指定ip地址和端口的相同)来更改此设置。 See https://github.com/TurboGears/gearbox#gearbox-http-servers and http://turbogears.readthedocs.io/en/latest/turbogears/gearbox.html#changing-http-server for additional details. 有关其他详细信息,请参见https://github.com/TurboGears/gearbox#gearbox-http-servershttp://turbogears.readthedocs.io/en/latest/turbogears/gearbox.html#changing-http-server

Note that wsgiref is the development server for TurboGears and usage on production is usually discouraged. 请注意, wsgiref是TurboGears的开发服务器,通常不建议将其用于生产。 You should consider using something like waitress, chaussette or mod_wsgi when deploying your application, see http://turbogears.readthedocs.io/en/latest/cookbook/deploy/index.html?highlight=deploy 部署应用程序时,您应该考虑使用女服务员,薄饼或mod_wsgi之类的东西,请参阅http://turbogears.readthedocs.io/en/latest/cookbook/deploy/index.html?highlight=deploy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM