我怎样才能用扭曲的Jinja? [英] How can I use Jinja with Twisted?

查看:225
本文介绍了我怎样才能用扭曲的Jinja?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在计划一个使用Twisted,Storm和Jinja的Python的讨论软件。问题是Jinja并不是为Twisted或者异步套接字库而制作的,使用Twisted所提供的性能是为什么我不打算使用Flask。

因此,如何使用Jinja Twisted渲染网页?

解决方案

您可以使用Jinja渲染网页,就像使用任何其他Twisted中的Python库。你只需要打电话这对Twisted来说可以很好地工作,但是如果Jinja做了一些阻塞操作,你可能会遇到性能问题。请注意,通过 deferToThread 或者只是阻止主循环(如果这不是性能问题),可以使用Twisted的阻塞库。所以,我推断你的问题实际上是如何在不阻塞的情况下使用Jinja。



Jinja是一个模板库,意思是它读取一个模板,调用一些视图模板上的逻辑,并写入一些HTML输出。所以有三件事情可以阻止:


  1. 阅读模板,
  2. 写结果。

  3. 运行视图逻辑(您的应用程序代码),

我不知道Jinja ,所以我不清楚这些API的API是如何构建的,我不能告诉你该怎么做,但是我的猜测是这个部分很简单,所以,我要给你一个关于第三方模板库和Twisted的一般答案。

所以我会解决这些问题中的每一个,尽管不太完全订单:

1。阅读模板



真的,这里最合理的做法是不关心它。阅读模板可能真的很快。这些是经常访问的小文件,您的操作系统几乎可以保存在文件系统缓存中。除非你把它们放在NFS上,否则你不可能阻止他们读取它们。如果你分析你的应用程序,并发现这是一个问题 - 因为,假设你的磁盘或远程文件系统非常慢 - 只需将模板读入 cStringIO 在启动时间类似,然后喂给忍者。

3。编写响应



Web页面并不是那么大,而Twisted不提供阻塞API来写入套接字。相反,它提供了一个API,它只是将整个结果缓存到内存中,直到可以写出来。我的建议是在读取模板时做同样的事情:除非你有很大的输出,在响应被送到客户端的时候烧掉一点内存很可能是正常的。



2。运行你的视图逻辑



这是你最有可能遇到问题的地方。 Jinja可能不会处理 Deferred s的结果。但实际上,Jinja并不是直接给你带来问题的,而是Storm。当你访问某些属性时,Storm希望能够阻止,进行数据库查询。与数据库块交谈,这是大多数Web应用程序阻塞I / O的最显着的来源。所以你需要决定如何处理这个问题。你有几个选择:


  1. 只需在主线程中执行,不用担心。也许你的应用程序是一个10人的工作组,你的数据库是本地的。当然,你的I / O将会阻止,但是如果它仍然满足它的性能要求,谁在乎呢?不是每个应用程序都需要缩放到月球和背部。
  2. deferToThread 调用(或类似的)并确保Jinja只访问内存中的对象。这样,你可以在你的主线程中执行你的渲染器,在执行数据库I / O的 Deferred 中进行回调。如果你只是访问内存中的对象,你的渲染器仍然可能需要一点时间,但没关系。这个问题促使我发表文章到我的博客关于阻塞和运行之间的区别这已经被搁置了一段时间了,您可能需要阅读它。

  3. 在线程或子进程中执行整个渲染,并将其视为程序的阻塞组件。这失去了使用Twisted的一些好处,但是将阻塞的Jinja / Storm组件和非阻塞的纯Twisted组件(实际的聊天消息中继部件)集成在一起仍然是一个完美可行的策略。

如果这些选项都不适用于您,自11.0版以来,Twisted已经包含了一个模板库,它支持 Deferred s 。您可以考虑使用 twisted.web.template 作为Jinja的替代品。


I'm planning up a discussion software using Python with Twisted, Storm, and Jinja. The problem is that Jinja was not made for Twisted or asynchronous socket libraries, and the performance provided by using Twisted is why I don't plan on using Flask.

So, how can I have Twisted render webpages using Jinja?

解决方案

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 possible to use blocking libraries with Twisted just fine, either via deferToThread 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.

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:

  1. reading the template,
  2. writing the result.
  3. running the view logic (your application code),

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.

So I'll address each of these concerns, although not quite in order:

1. Reading the Template

Really the most reasonable thing to do here is to not care about it. 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 cStringIO or something similar at startup time and feed it to jinja after that.

3. Writing the Response

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.

2. Running your View Logic

This is the area where you are most likely to run into problems. Jinja probably doesn't handle the results of Deferreds. 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:

  1. 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.
  2. Pre-fetch everything from storm in a deferToThread 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 Deferred 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 post an article to my blog about the distinction between "blocking" and "running" which had been hanging out as a draft for quite a while; you may want to go read it.
  3. 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.

If none of these options work for you, Twisted has included a templating library that does support Deferreds since version 11.0. You may consider using twisted.web.template as an alternative to Jinja.

这篇关于我怎样才能用扭曲的Jinja?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆