Rendering templates in Queue (ie, without RequestHandlers)

151 views
Skip to first unread message

Mike MacCana

unread,
Jul 4, 2010, 12:02:16 PM7/4/10
to python-...@googlegroups.com
Hi all,

I have Tornado throwing posted messages onto a Queue, then a Thread object with MessageMixin which runs continuously processing the messages and responding to clients.

It works quite nicely, but I'm not using the render_string - render_string() is provided by RequestHandler, which needs a Request in its constructor. So I don't get any escaping. 

Interestingly I checked OReilly's Realtime book and their Tornado Twitter example (which also uses Queues) doesn't use escaping either, presumably for the same reason.

Anyone else using Queues? How are you escaping?

Is there a good way to access render_string() outside a request ?

Mike
 


Jehiah Czebotar

unread,
Jul 4, 2010, 1:11:12 PM7/4/10
to python-...@googlegroups.com
by "access render_string() outside of a request" i think you are
really asking, is there a way to render a template to a string outside
a request; the answer is yes.

You can write code that uses tornado.template just like render_string
does, or if it's really just escaping you are looking for, you can
access all the metods of tornado.escape directly just like you can
from inside rendering a template.

a very basic render_template for use outside of the request handler
would look like this

{{{
from tornado import template
def render_template(template_name, **kwargs):
loader = template.Loader("/path/to/templates")
t = loader.load(template_name)
return t.generate(**kwargs)
}}}

you can find more about what render_string does by looking at the source here.
http://github.com/facebook/tornado/blob/master/tornado/web.py#L430

--
Jehiah

David P. Novakovic

unread,
Jul 4, 2010, 6:07:17 PM7/4/10
to python-...@googlegroups.com
Out of interest, what does this situation provide beyond allowing naughty longer running requests to be interrupted/context switched?

D

Stanislav

unread,
Jul 5, 2010, 5:41:01 PM7/5/10
to Tornado Web Server
You should really test the speed of this, I am quite sure that if you
use AB to benchmark this it will be responding slower then a normal
Tornado app that renders within the request. Python and threads = big
no no

On Jul 4, 3:07 pm, "David P. Novakovic" <davidnovako...@gmail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages