I am trying to integrate Faust with Django to publish the messages to Kafka. Here is the example in Faust repo: https://github.com/robinhood/faust/tree/master/examples/django
I modified it a bit, and created views to push data to Kafka via Faust.
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")
But, I get this error now:
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
I am running this Django app using development server. What am I doing wrong? Anyone? :)
Faust is a stream processing library, porting the ideas from Kafka Streams to Python. It is used at Robinhood to build high performance distributed systems and real-time data pipelines that process billions of events every day.
See the FAQ section in faust documentation
Can I use Faust with Django/Flask/etc.?
Yes! Use eventlet
as a bridge to integrate with asyncio
.
Using eventlet
This approach works with any blocking Python library that can work with eventlet
.
Using eventlet
requires you to install the aioeventlet
module, and you can install this as a bundle along with Faust:
$ pip install -U faust[eventlet]
Then to actually use eventlet as the event loop you have to either use the -L <faust --loop>
argument to the faust
program:
$ faust -L eventlet -A myproj worker -l info
or add import mode.loop.eventlet
at the top of your entry point script:
#!/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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With