robinhood / faust

Python Stream Processing
Other
6.72k stars 535 forks source link

Neither faust[gevent] or faust[eventlet] work with Python 3.7 #272

Open arel opened 5 years ago

arel commented 5 years ago

Checklist

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 and aiogevent integrations. Both libraries fail.

Attempting to install pip install faust[gevent] fails completely (the project seems to have been removed from PyPI) #182.

Collecting aiogevent
  Could not find a version that satisfies the requirement aiogevent (from versions: )
No matching distribution found for aiogevent

So I tried pip install faust[eventlet], which also fails with a Syntax error at import time (with Python 3.7).

In [1]: import aioeventlet                                                                                                                   
Traceback (most recent call last):

  File "/home/ubuntu/.local/share/virtualenvs/b-YiSOAUOy/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3267, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)

  File "<ipython-input-1-ebb10c4f53e9>", line 1, in <module>
    import aioeventlet

  File "/home/ubuntu/.local/share/virtualenvs/b-YiSOAUOy/lib/python3.7/site-packages/aioeventlet.py", line 332
    future = asyncio.async(future, loop=loop)
                         ^
SyntaxError: invalid syntax

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

stevanmilic commented 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.

databasedav commented 5 years ago

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!

stevanmilic commented 5 years ago
  1. You don't need eventlet loop
  2. faust_agent.send is already using async library, so no need to port it

Example:

@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.

lsabi commented 5 years ago

@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

arel commented 5 years ago

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.

lsabi commented 5 years ago

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)

yeralin commented 4 years ago

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

mainakchhari commented 4 years ago

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

lightoyou commented 4 years ago

Up on this : File "/usr/local/lib/python3.7/site-packages/aioeventlet.py", line 332 future = asyncio.async(future, loop=loop) ^ SyntaxError: invalid syntax

lightoyou commented 4 years ago

Who can help me to fix ?

rotten commented 4 years ago

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?

dzlabs commented 3 years ago

also an issue for us

iXioN commented 2 years ago

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 ?

mashkovd commented 2 years ago

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
rotten commented 2 years ago

FWIW, we switched to the faust-streaming fork and haven't looked back: https://github.com/faust-streaming/faust