Open arel opened 5 years ago
async
and await
are reserved keywords in Python 3.7, thus the error. I suggest using sync_to_async that Andrew wrote.
So if using eventlet
with Flask
, is doing something like this:
async def func():
await sync_to_async(faust_agent.send)(...)
and running the faust worker with faust -L eventlet -A myproj worker -l info
what you mean? An example would really be appreciated thanks!
eventlet
loopfaust_agent.send
is already using async library, so no need to port itExample:
@app.agent()
async def add_account(accounts: StreamT[AccountRecord]):
async for account in accounts:
await create_account(account)
@sync_to_async
def create_account(account: AccountRecord):
Account.objects.create(
name=account.name,
score=Decimal(str(account.score)),
active=account.active,
)
create_account
function is decorated with sync_to_async
, but you could use await sync_to_async(...)(...)
syntax too.
Check Andrew's blog post for more info, especially Sync From Async section.
@arel now pip3 install faust[eventlet]
works, while faust[gevent] does not.
Also, do you have a working example of how you did it? I'm having some hard times understanding how to use both flask and faust together.
Thanks
Thanks, @lsabi. I don't have a working example; I ended up not using flask and faust together, but I would still like to see a working example if anyone else has one.
The same from my side. I started using faust as standalone server (which, I have to say, is not bad), but I agree about the example of faust + flask.
Or even in the most general case, a working example on how to use faust[gevent] / faust[eventlet] outside django (which is different from other frameworks such as flask)
Hey @ask any ideas on this?
I'm also trying to integrate faust as a module to my aiohttp server. Tried using aioeventlet
according to FAQ, but now it throws:
future = asyncio.async(future, loop=loop)
^
SyntaxError: invalid syntax
python==3.7.5
Is there any word on this? I am trying to run the simplest greeting example in Quickstart with faust[eventlet] and starting the worker with -L eventlet
. I am running the example with a docker image built from python:3.7-latest and the worker fails with File "/usr/local/lib/python3.7/site-packages/aioeventlet.py", line 332 future = asyncio.async(future, loop=loop) ^ SyntaxError: invalid syntax
Up on this : File "/usr/local/lib/python3.7/site-packages/aioeventlet.py", line 332 future = asyncio.async(future, loop=loop) ^ SyntaxError: invalid syntax
Who can help me to fix ?
I noticed when I hover over the "asyncio" word in the latest FAQ it says (in Python 3.9)
. Does this mean it only works with a version of Python that is still in development?
also an issue for us
As I can see there are still no fix on that, I'm trying to fix this since 2 days and still without any result.
My final use case will be to my_topic.send()
from django but I still can do it properly because I get or FuturMessage or some kind of promise. Even when I try t use the my_topic.send_soon()
which seems to do what I'm trying to do.
Does anyone already sent value in a topic from a Django app here ?
thx for post follow working example
import faust
from faust.types import StreamT
from faustapp.app import app
from .models import Account
from asgiref.sync import sync_to_async
class AccountRecord(faust.Record):
name: str
score: float
active: bool
@app.agent()
async def add_account(accounts: StreamT[AccountRecord]):
async for account in accounts:
await create_account(account)
@sync_to_async
def create_account(account):
result = Account.objects.create(
name=account.get('name'),
score=Decimal(str(account.get('score'))),
active=0)
return result.pk
FWIW, we switched to the faust-streaming fork and haven't looked back: https://github.com/faust-streaming/faust
Checklist
master
branch of Faust.Steps to reproduce
I have been trying to integrate faust with a flask app, and following the documentation (https://faust.readthedocs.io/en/latest/faq.html) I have tried both
aioeventlet
andaiogevent
integrations. Both libraries fail.Attempting to install
pip install faust[gevent]
fails completely (the project seems to have been removed from PyPI) #182.So I tried
pip install faust[eventlet]
, which also fails with a Syntax error at import time (with Python 3.7).I'm new to
asyncio
so I'm finding it confusing to integrate faust with flask, and it would be helpful to update the documentation to provide a working example, or at least convey the limitations of these libraries with Python 3.7.Expected behavior
I would expect the examples and references in the documentation to work on Python 3.7.
Versions