0

I would like to read a file, update the website, read more lines, update the site, etc ...The logic is below but it's not working. It only shows the first line from the logfile and stops. Is there a way to iterate over 'return render_to_response'?

#django view calling a remote python script that appends output to the logfile

proc = subprocess.Popen([program, branch, service, version, nodelist])
logfile = 'text.log'
fh = open(logfile, 'r')

while proc.poll() == None:
  where = fh.tell()
  line = fh.read()
  if not line:
     time.sleep(1)
     fh.seek(where,os.SEEK_SET)
  else:
     output = cgi.escape(line)
     output = line.replace('\n\r', '<br>')
     return render_to_response('hostinfo/deployservices.html', {'response': output})

Thank you for your help.

Eevee
  • 47,412
  • 11
  • 95
  • 127
  • Wow. You can return from function only one time per call. It isn't even python-specific – neoascetic Jul 31 '12 at 23:06
  • What you're trying to do is complicated and requires threading and an AJAX poll... hard to answer in full. – mVChr Aug 01 '12 at 00:29
  • That's a resounding 'no' I take it :). Oh well, was just trying my luck shooting for the simplest answer. Back to googling board. – Trisha Hoang Aug 01 '12 at 01:19

3 Answers3

1

You can actually do this, by making your function a generator - that is, using 'yield' to return each line.

However, you would need to create the response directly, rather than using render to response.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
0

render_to_response will render the first batch to the website and stop. Then the website must call this view again somehow if you want to send the next batch. You will also have to maintain a record of where you were in the log file so that the second batch can be read from that point.
I assume that you have some logic in the templates so that the second post to render_to_response doesnt overwrite the first
If your data is not humongous, you should explore sending over the entire contents you want to show on the webpage each time you read some new lines.

zaphod
  • 2,045
  • 1
  • 14
  • 18
  • Since the log will be small, I was thinking of aggregating the output 'output += line' and update that each time ... – Trisha Hoang Aug 01 '12 at 23:01
  • Since the log is small, I would yet suggest going for the easy solution of re-generating the log page each time. – zaphod Aug 02 '12 at 19:01
0

Instead of re-inventing the wheel, use django_logtail

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284