Forums

Django webapps speed

I am experimenting with a Django web application hosted on Pythonanywhere under the free subscription. The application is programmed to make GET requests roughly every second in order to update a live graph on the web page. While the application works fine, I am a little concerned with the speed. The updates happen every 2-3 seconds as opposed to every second (This slowdown doesn't happen when I test run the app on my local machine).

How should I go about reducing this update time? If I upgrade my subscription to some of the paid plans will this speed things up? Or the problem lies elsewhere?

It's hard to answer without knowing a bit more about the specifics of what you're doing -- how do the get requests happen? is it some sort of client-side javascript call that loops every second?

One reason you might get a speedup from moving to a paid plan is that the free account only has one web worker, so it can only deal with one request at a time. Maybe the reason you're seeing slowdowns is because answering one query takes more than one second -- you could put a few debug prints into your code to try and measure what's taking time.

Our general advice for speedup is to make sure you're not doing things in python that you could be doing in the database. And the one thing which is likely to be slower on pythonanywhere than on your local pc is filesystem access, so if you're reading and writing a lot of files (uploads? writing data to disk?), that could be slower than it is on your own pc...

Thanks for the prompt reply. Just to follow up on the extra workers that come with the paid subscription -- do they require some sort of parallel programming (if yes could you provide a link to a example/tutorial) or is it all handled automatically?

About the GET requests - I have a simple ajax/javascript on the client side that updates a few variables every second (see below). On the server side there is no writing /saving files. The code simply queries the sqlite database for values and returns a json object which then updates a graph on the page and a few html fields. Here is the javascript on the client side:

var interval;
function update(){
$.ajax({
    type: "GET",
    url:'/bets/update/',
    data: {category_id:5,zzz:5 },
    datatype: 'json',
    success: function(data){

    var asset_price = data['eurusd'];
    $('#current_price').html(asset_price);
    $('#current_time').html(data['time']);
    $('#expire').html(data['expire']);
    var oilprices1 = data['oilprice1'];
       <!-- There is function here that plots the graph -->

    interval = setTimeout(update, 1000);
        }

    });
}

You don't need to do anything special to handle the extra workers -- they're basically just separate copies of your code, running in separate processes. So unless you're trying to do something complicated like communicate between a request from one user to a request from another user without going through the database (definitely not recommended!) then you should be fine.

One other possibility regarding the speed -- local-to-remote is always going to be at least a little slower than local-to-local, just because of basic network lag. Try pinging your web app to see what the round-trip time across the network is.