Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can render web pages using Jinja the same way you would use any other Python library in Twisted. You just call into it. This will work fine with Twisted, although you may run into performance issues if Jinja does something blocking. Note that it is <em>possible</em> to use blocking libraries with Twisted just fine, either via <code>deferToThread</code> or just blocking the mainloop if it's not a performance problem. So, I am inferring that your question is really about how to use Jinja without blocking.</p> <p>Jinja is a templating library, which means that it reads a template, invokes some view logic on the template, and writes some HTML output. So there are 3 things that can block:</p> <ol> <li>reading the template,</li> <li>writing the result.</li> <li>running the view logic (your application code),</li> </ol> <p>I don't know Jinja, so I don't know exactly how the APIs for each of these things is structured and I can't tell you what to do, but my guess is that that part's easy; so, I'm going to give you a general answer about 3rd-party templating libraries and Twisted.</p> <p>So I'll address each of these concerns, although not quite in order:</p> <h1>1. Reading the Template</h1> <p>Really the most reasonable thing to do here is to <strong>not care about it</strong>. Reading the template is probably really fast. These are frequently-accessed, tiny files, which your operating system is almost certainly keeping in its filesystem cache. It's unlikely that you're ever going to block on reading them unless you're doing something nuts like putting them on NFS. If you profile your application and find that this is a problem – because, let's say, you have extremely slow disks or a remote filesystem – just read the template into a <code>cStringIO</code> or something similar at startup time and feed it to jinja after that.</p> <h1>3. Writing the Response</h1> <p>Web pages are not all that big, and Twisted doesn't provide a blocking API to write to sockets. Instead, it offers an API which just buffers the whole result in memory until it can be written out. My suggestion is to do basically the same thing here as with reading the template: unless you have seriously huge output, it's probably fine to burn up a little bit of RAM while the response is being fed to the client.</p> <h1>2. Running your View Logic</h1> <p>This is the area where you are most likely to run into problems. Jinja probably doesn't handle the results of <code>Deferred</code>s. But actually, it's not actually Jinja which is going to give you problems directly: it's Storm. Storm expects to be able to block, to make database queries, when you access certain attributes. Talking to the database blocks, and this is the most signifiant source of blocking I/O in most web applications. So you need to decide how you're going to deal with that. You have a few options:</p> <ol> <li>Just do it in the main thread and don't worry about it. Maybe your application is for a workgroup of 10 people and your database is local. Sure, your I/O is going to block, but if it still meets its performance requirements, who cares? Not every application has to scale to the moon and back.</li> <li>Pre-fetch everything from storm in a <code>deferToThread</code> call (or similar) and make sure Jinja is only accessing objects in memory. This way you can run your renderers in your main thread, in a callback on a <code>Deferred</code> that was doing database I/O. If you're only accessing objects in memory, your renderer still might take a little while, but that's fine. This question prompted me to <a href="http://glyph.twistedmatrix.com/2011/11/blocking-vs-running.html">post an article to my blog about the distinction between "blocking" and "running"</a> which had been hanging out as a draft for quite a while; you may want to go read it.</li> <li>Do your whole render in a thread or subprocess, and treat it as a blocking component of your program. This loses some of the benefits of using Twisted, but it's still a perfectly viable strategy to integrate a blocking Jinja/Storm component and a non-blocking pure-Twisted component, the actual chat-message relaying part.</li> </ol> <p>If none of these options work for you, <a href="http://twistedmatrix.com/documents/current/web/howto/twisted-templates.html">Twisted has included a templating library that <em>does</em> support <code>Deferred</code>s</a> since version 11.0. You may consider using <code>twisted.web.template</code> as an alternative to Jinja.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload