如何整合浮士德与德扬戈?

2024-07-04 05:54:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图将浮士德与德扬戈结合起来,将这些信息发布给卡夫卡。 以下是《浮士德报告》中的例子:https://github.com/robinhood/faust/tree/master/examples/django

我对它做了一些修改,创建了视图,通过浮士德将数据推送到卡夫卡

from django.shortcuts import render

from asgiref.sync import async_to_sync

from accounts.agents import AccountRecord, add_account


async def send_data() -> None:
    print("sending..data")
    print(await add_account.ask(AccountRecord(name="tesst", score=10.9, active=False)))

def index(request):
    async_to_sync(send_data)()
    return render(request, "accounts/index.html")

但是,我现在得到了这个错误:

RuntimeError at / Task <Task pending name='Task-1' coro=<AsyncToSync.main_wrap() running at /Users/mysuer/.pyenv/versions/3.8.3/envs/faustdjango/lib/python3.8/site-packages/asgiref/sync.py:204> cb=[_run_until_complete_cb() at /Users/mysuer/.pyenv/versions/3.8.3/lib/python3.8/asyncio/base_events.py:184]> got Future attached to a different loop

我正在使用开发服务器运行这个Django应用程序。 我做错了什么? 有人吗?:)


Tags: todjangofromimportaddtaskdataasync
1条回答
网友
1楼 · 发布于 2024-07-04 05:54:00

请参阅faust documentation中的常见问题部分

我可以将浮士德与Django/Flask/等一起使用吗?

对!!使用eventlet作为与asyncio集成的桥梁

使用eventlet

这种方法适用于任何可以使用eventlet的阻塞Python库

使用eventlet需要安装aioeventlet模块,您可以将其作为捆绑包与Faust一起安装:

$ pip install -U faust[eventlet]

然后,要实际使用eventlet作为事件循环,您必须使用-L <faust loop>程序的faust参数:

$ faust -L eventlet -A myproj worker -l info

或者在入口点脚本的顶部添加import mode.loop.eventlet

#!/usr/bin/env python3
import mode.loop.eventlet  # noqa

Warning It's very important this is at the very top of the module, and that it executes before you import libraries.

相关问题 更多 >

    热门问题