将 Flask 与 Faust 集成

Integrate Flask with Faust

我正在尝试让浮士德特工在烧瓶中投射信息 view/endpoint, 我找不到任何例子,我真的很挣扎。

有人试过成功吗? 文档说使用 gevent 或 eventlet 作为通往 asyncio 的桥梁,但不幸的是无法理解

您可以尝试 monkeypatch 您的 flask 应用程序使用 gevent 或 eventlet。据我所知,eventlet 是目前唯一可用的,因为桥 aiogevent 在 pip 中不再可用。

另一种方法是委托您的 Flask 应用仅生成带有 pykafka or kafka-python 的消息。通过这种方式,您将保持两个应用程序隔离,flask 生产和 faust 消费。请注意,默认情况下,faust 将尝试创建没有 internal 标志且具有 1 个副本的主题和具有 8 个副本的内部主题。

faust 文档有一个集成 flask 和 faust 的建议。它涉及使用 gevent,但我认为它不需要 monkeypatching。

来自文档 (https://faust.readthedocs.io/en/latest/faq.html):

This approach works with any blocking Python library that can work with gevent.

Using gevent requires you to install the aiogevent module, and you can install this as a bundle with Faust:

$ pip install -U faust[gevent]
Then to actually use gevent as the event loop you have to either use the -L option to the faust program:

$ faust -L gevent -A myproj worker -l info
or add import mode.loop.gevent at the top of your entry point script:

#!/usr/bin/env python3
import mode.loop.gevent
REMEMBER: It’s very important that this is at the very top of the module, and that it executes before you import libraries.