Streaming console output

329 views
Skip to first unread message

Paul Wray

unread,
Apr 14, 2010, 9:23:24 PM4/14/10
to web2py-users
Hello
I would like to make some administative build/test tasks for a web2py
application available via the web interface, with the output presented
to the html page in real time, like console output.

The code below seems to work reasonably well, but is this sort of
thing recommended/safe? Is there a better way?
I am running windows.

import time
import threading

class my_thread(threading.Thread):
def __init__(self, file):
self.file = file
threading.Thread.__init__(self)

def run(self):
self.file.write('<html><body>')
# test streamed html output
for i in range(10):
self.file.flush()
time.sleep(1) # simulate long-running process
# pad lines to be more than a chunk
self.file.write('<p>%100d </p>' % (i,))
self.file.write('</body></html>')
self.file.close()


def my_controller():

(rh, wh) = os.pipe()
r = os.fdopen(rh, 'r')
w = os.fdopen(wh, 'w')
response.headers['Content-Type'] = 'text/html'

my_thread(w).start()
return response.stream(r, 50) # set chunk size to something
small

mdipierro

unread,
Apr 14, 2010, 9:55:03 PM4/14/10
to web2py-users
I do not seem an immediate problem except the fact that a thread
started by the web server starts another thread and it may get killed
for long running tasks. I suggest you look into the admin/controller/
shell.py

Massimo

Reply all
Reply to author
Forward
0 new messages