0

I'm writing a code where users can manage servers (AWS instances) from a browser. The servers take a while to start up and I would like to do a live countdown so users know how long to wait for before all the servers are turned on.

This is my views.py code

def startservers(request, question_id):
    inst() #updates all instance status
    if instance1[0].state in 'stopped':
        instance1[0].start()
    if instance2[0].state in 'stopped':
        instance2[0].start()
    if instance3[0].state in 'stopped':
        instance3[0].start()
    if instance4[0].state in 'stopped':
        instance4[0].start()
    for i in range (10,0,-1):
        print 'Wait for %d seconds \r' % i,
        sys.stdout.flush()
        time.sleep(1)
    check() #makes sure all instances are running
    text1 = 'WebServer-Test %s' % instance1[0].state
    text2 = 'Gluster %s' % instance2[0].state
return render(request, 'awsservers/startservers.html', {'webstatus': text1, 'glu1status': text2})

I'm not sure how I should parse the countdown value to

  return render(request, 'awsservers/startservers.html', {'webstatus': text1, 'glu1status': text2})

To understand better what I mean by a live countdown, you can just run this section of the code on a console to see the output it produces.

for i in range (10,0,-1):
        print 'Wait for %d seconds \r' % i,
        sys.stdout.flush()
        time.sleep(1)
print '' #without this, next line prints onto the same line as "Wait for.."
print "Server1 running"
print "Server 2 running"

Thanks for the help!

If anyone knows a much better way to put up a countdown, do let me know!

carol Alex
  • 353
  • 1
  • 3
  • 15

1 Answers1

1

One simple way to do this is via ajax. Using javascript make a call to your method every second and display the response. In this case you probably don't need a loop on the server side and can just return the state... If the server is started, display the appropriate text, if it is not show the countdown.

The JavaScript code will be something like this

var i = 10;
var timer = window.setInterval(function(){
  $.ajax({
    url: // your url
    data: // any parameters you need to pass as JSON
    success: function(response){
        if (response == 'started'){
            $('#server_status').text('Wait for ' + i + ' seconds')
            i--;
        }else
            $('#server_status').text('running')
  }});
  if (i == 0)
        window.clearInterval(timer);
}, 1000);   // Repeat every 1 second

Please note that I have used JQuery for the ajax call

The other way might be to use a StreamingHttpResponse

In this case your loop should be separated into a method and the returned value should be sent to the html page or as the response

def count_seconds():
    for i in range (10,0,-1):
        time.sleep(1)
        yield 'Wait for %d seconds \r' % i,

The yield keyword will return the ith value every time the function is called.

Resley Rodrigues
  • 2,218
  • 17
  • 17
  • I agree; but if we want updates every second we have to check every second – Resley Rodrigues Jan 08 '16 at 05:29
  • 1
    Well there is an option called websockets... It somewhat easy to setup... And the result you would agree – Raja Simon Jan 08 '16 at 05:32
  • Sorry for the late reply on this, I did end up using AJAX but the time interval wasn't working out for me so I ended up using a button click instead and it works fine!. I'll accept your answer for guiding me to use AJAX :) – carol Alex Jan 17 '16 at 23:18