- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要创建一个网络应用程序来显示通过定期传入的 UDP 数据包提供的数据。该站点可能位于 Flask(可能是 Pyramid)中,部署在 Nginx 下。我如何创建一个非常简单的后台任务(基本上只是 socket.recv())来监听任何传入的数据包,并将数据推送到全局可访问的列表中?
我可以简单地从 main() 生成一个线程来执行此操作,还是我需要使用 Celery 或 PyRes 之类的东西?
感谢您的指导。
最佳答案
你将不得不使用 celery ,但你很幸运,因为 There's already a flask extension that integrates celery .你必须 pip install flask
, pip install flask-celery
, pip install redis
并且你的系统需要一个redis服务器.
import socket, select, Queue
from flask import Flask
from celery import Celery
def make_celery(app):
celery = Celery(app.import_name, broker=app.config['CELERY_BROKER_URL'])
celery.conf.update(app.config)
TaskBase = celery.Task
class ContextTask(TaskBase):
abstract = True
def __call__(self, *args, **kwargs):
with app.app_context():
return TaskBase.__call__(self, *args, **kwargs)
celery.Task = ContextTask
return celery
app = Flask(__name__)
app.config.update(
CELERY_BROKER_URL='redis://localhost:6379',
CELERY_RESULT_BACKEND='redis://localhost:6379'
)
celery = make_celery(app)
socket_queue = Queue.Queue()
@celery.task()
def listen_to_udp():
"""
This code was taken from
https://stackoverflow.com/questions/9969259/python-raw-socket-listening-for-udp-packets-only-half-of-the-packets-received
"""
s1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s1.bind(('0.0.0.0', 1337))
s2 = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_UDP)
s2.bind(('0.0.0.0', 1337))
while True:
r, w, x = select.select([s1, s2], [], [])
for i in r:
socket_queue.put((i, i.recvfrom(131072)))
@app.route("/")
def test_home():
listen_to_udp.delay()
print(socket_queue.get())
if __name__ == "__main__":
#run install.py to install dependencies and create the database
app.run(host="0.0.0.0", port=5000, debug=True)
关于python - Flask 或 Pyramid 中的简单网络 UDP 监听,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26281935/
Pyramid 项目中有一个 development.ini 或 production.ini 。我将自己的配置数据添加到 ini 文件如: [thrift] host = 0.0.0.0 port
我想注册一个请求处理程序,但不想使用扫描方法。 为什么我需要调用两个方法(add_route 和 add_view)而不是一个? from wsgiref.simple_server import m
请问我错过了什么。当我想提供已下载到磁盘上的视频文件时,我不断在浏览器中收到内部服务器错误。这是我的代码: View 函数 @view_config(name='download_video') de
请问我错过了什么。当我想提供已下载到磁盘上的视频文件时,我不断在浏览器中收到内部服务器错误。这是我的代码: View 函数 @view_config(name='download_video') de
我目前正在学习如何使用 Python Pyramid Web 框架,并且发现文档非常出色。 然而,在区分“模型”(即在 SQLAlchemy 声明性系统下定义的类)的概念和“资源”(即定义访问控制的方
我一直在尝试让 Pyramid 在谷歌应用程序引擎中运行,但没有成功。我尝试按照说明 here 进行操作但它似乎已经过时了,因为 gae 不再有 appcfg.py 了。我按照应用程序引擎文档中的 F
大多数可用教程都展示了如何使用上游 HTTP 服务器(如 NGINX)设置 uWSGI。但是 uWSGI 本身就可以完美地充当路由器/代理/负载均衡器 - 请参阅 this对于我的项目,我现在不想设置
我正在尝试使用 Pyramid 自省(introspection)接口(interface)从可调用 View 中获取给定资源类型的所有 View 的列表。我可以使用以下方法获取一组已添加的 View
我正在使用 Pyramid 来创建网络应用程序。然后我使用 Pyramid 烧杯将烧杯连接到 Pyramid 的 session 管理系统。 有两个值会影响用户 session 的持续时间。 sess
背景 我对 unicode 和 Python 真是一团糟。这似乎是一个普遍的焦虑,我尝试过使用其他解决方案,但我就是无法解决这个问题。 设置 MySQL 数据库设置 collation_datab
模型 - View - PHP 框架(如 Kohana)的 Controller 的 Pyramid/Python 等价物是什么? In Pyramid "Model" is .... and it
我遵循了 http://docs.pylonsproject.org/docs/pyramid/en/latest/tutorials/wiki/index.html 上的教程 我知道,当我添加或更改
我使用 yapps 为 Pyramid 内的 LaTex 语言生成解析器(例如将 \begin{itemize} 之类的内容翻译成相应的 -Tags)。一个命令(即 \ref{SOMEID} )应该
我正在 Pyramid 框架之上使用 python 制作 webapps。 在我利用 Mechanize 进行一些简单网页抓取的函数之一中,当我将其作为独立的 Python 脚本运行并通过 Pyram
var z = []; for(var i = 1; i len) z.push("a".repeat(len-i%len)) console.log(z.join("\n")); 关于jav
我正在开发 Python Pyramid我需要使用rest api,在其中一个请求中,我需要处理一个excel,对于它的每一行,我都会获取GPS坐标并进行大量验证,这意味着这个唯一的请求可能需要大约1
这是我的 base.html: {% block head %} {% endblock %} {% block body
我一直在尝试使用 Pyramid 框架制作带有复选框和单选按钮的表单,但我不知道如何正确执行。 我正在使用pyramid_simpleform。到目前为止,我已经能够使用 for 循环将复选框放在表单
我按照 Pyramid 教程进行操作,一切正常。然后我为 Pyramid 安装了 jinja2,并将必要的代码行添加到我的 development.ini 文件中。按预期在指定位置找到了我的模板。它们
我的应用程序从用户接收一个或多个 URL(通常为 3-4 个 URL),从这些 URL 中抓取某些数据并将这些数据写入数据库。但是,因为抓取这些数据需要一点时间,所以我正在考虑在单独的线程中运行每个抓
我是一名优秀的程序员,十分优秀!