pythonajaxconcurrencyturbogears2

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.

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).

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. To refresh N values takes 3*N seconds where it could just take 3 seconds with concurrency.

Any idea how to fix this ?

Here is my current server-side code (method get_load is the one called with 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

Solution

  • Your problem is probably caused by the fact that you are serving requests with Gearbox wsgiref server. By default the wsgiref server is single threaded and so can serve a single request at time. 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). 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.

    Note that wsgiref is the development server for TurboGears and usage on production is usually discouraged. 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