本文介绍了Python中的非消息队列/简单长查询(和Flask)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种简单的方法(即,不需要我设置一个单独的服务器来处理消息传递队列),以便对运行计算和生成图形的小型Web界面进行长轮询。这是我的网络界面需要做的:
$ b $ ol
  • 用户请求web界面中的图形/数据

  • 服务器运行一些计算。
  • 在服务器运行计算的同时,一个小容器被更新(可能通过AJAX / jQuery),计算进度类似于 (如打印计算密度函数...))

  • 计算完成并显示给用户。

  • ol>

    由于计算全部在服务器端完成,所以我不确定如何轻松设置。很明显,我将要设置一个REST API来处理轮询,这在Flask中很容易。但是,我不知道如何检索实际的更新。一个显而易见的,虽然这个目的很复杂,解决方案将是建立一个消息队列,并进行一些长的轮询。不过,我不确定这是否正确。



    以下是我的问题:


    1. 有没有办法使用文件系统?性能不是一个大问题。 AJAX / jQuery能从文件中找到消息吗?保存进度到一些.json文件?

    2. 酸洗是什么? (我并不是很了解酸洗,但是也许我可以腌制一个消息字典,并且可以通过处理轮询的API读取它。)
    3. 即使是正确的轮询进场?有更好或更普遍的模式来处理这个问题吗?

    我觉得我太过于复杂,因为我知道这种东西在网络上很常见。通常情况下,我会看到一些事情正在发生,而一些loading.gif图片正在运行,而一些计算正在进行(例如,在Google Analytics中)。

    感谢您的帮助!

    解决方案

    我使用Flask和jQuery构建了几个这样的应用程序。根据这个经验,我会说你的计划是好的。


    1. 不要使用文件系统。您将遇到JavaScript安全问题/保护。万一你找到合理的解决方法,你仍然不会有任何便携或可扩展的东西。相反,使用一个小的本地Web服务框架,如Flask。

    2. 使用JSON。这是Web应用程序和REST界面的语言。 jQuery和那些用于绘制图表,图形等的基于jQuery的好插件都需要JSON。它很容易使用,易于阅读,而且对于小规模的应用程序,没有理由去其他地方。
    3. 长轮询是对于你想完成的任务来说很好。纯粹的基于HTTP的应用程序有一些限制。而像Socket.IO这样的WebSockets和类似的socket-ish层是未来。但是根据我的经验,找到服务器端实现的很好,简单的例子是困难的。我看起来很难。有很多例子要你设置Node.js,REDIS和其他中间件。但为什么我们必须建立两个或三个独立的中间件服务器?这很可笑。因此,在Flask这样一个简单的,纯Python的Web框架上进行长时间轮询是走向国际海事组织(IMO)的一种方式。

    4. 代码是一个多一点,而不是一个片段,所以,而不是把它包括在这里,我已经把一个简化的例子到,您可以自由查看,复制或克隆。有三个部分:
      $ b $ ul
      $ lt; code> serve.py 一个基于Python / Flask的服务器
    5. templates / index.html 98%HTML,2%模板文件基于Flask的服务器将呈现为HTML

    6. $ li $ $ c $ static $ lpoll.js 一个基于jQuery的客户端

      I am looking for a simple (i.e., not one that requires me to setup a separate server to handle a messaging queue) way to do long-polling for a small web-interface that runs calculations and produces a graph. This is what my web-interface needs to do:

      1. User requests a graph/data in a web-interface
      2. Server runs some calculations.
      3. While the server is running calculations, a small container is updated (likely via AJAX/jQuery) with the calculation progress (similar to what you'd do in a consol with print (i.e. print 'calculating density function...'))
      4. Calculation finishes and graph is shown to user.

      As the calculation is all done server-side, I'm not really sure how to easily set this up. Obviously I'll want to setup a REST API to handle the polling, which would be easy in Flask. However, I'm not sure how to retrieve the actual updates. An obvious, albeit complicated for this purpose, solution would be to setup a messaging queue and do some long polling. However, I'm not sure sure this is the right approach for something this simple.

      Here are my questions:

      1. Is there a way to do this using the file system? Performance isn't a huge issue. Can AJAX/jQuery find messages from a file? Save the progress to some .json file?
      2. What about pickling? (I don't really know much about pickling, but maybe I could pickle a message dict and it could be read by an API that is handling the polling).
      3. Is polling even the right approach? Is there a better or more common pattern to handle this?

      I have a feeling I'm overcomplicating things as I know this kind of thing is common on the web. Quite often I see stuff happening and a little "loading.gif" image is running while some calculation is going on (for example, in Google Analytics).

      Thanks for your help!

      解决方案

      I've built several apps like this using just Flask and jQuery. Based on that experience, I'd say your plan is good.

      1. Do not use the filesystem. You will run into JavaScript security issues/protections. In the unlikely event you find reasonable workarounds, you still wouldn't have anything portable or scalable. Instead, use a small local web serving framework, like Flask.

      2. Do not pickle. Use JSON. It's the language of web apps and REST interfaces. jQuery and those nice jQuery-based plugins for drawing charts, graphs and such will expect JSON. It's easy to use, human-readable, and for small-scale apps, there's no reason to go any place else.

      3. Long-polling is fine for what you want to accomplish. Pure HTTP-based apps have some limitations. And WebSockets and similar socket-ish layers like Socket.IO "are the future." But finding good, simple examples of the server-side implementation has, in my experience, been difficult. I've looked hard. There are plenty of examples that want you to set up Node.js, REDIS, and other pieces of middleware. But why should we have to set up two or three separate middleware servers? It's ludicrous. So long-polling on a simple, pure-Python web framework like Flask is the way to go IMO.

      The code is a little more than a snippet, so rather than including it here, I've put a simplified example into a Mercurial repository on bitbucket that you can freely review, copy, or clone. There are three parts:

      • serve.py a Python/Flask based server
      • templates/index.html 98% HTML, 2% template file the Flask-based server will render as HTML
      • static/lpoll.js a jQuery-based client

      这篇关于Python中的非消息队列/简单长查询(和Flask)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    09-13 11:04