New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add asyncio.Queue __aiter__, __anext__ methods #72963
Comments
adding to asyncio.Queue class following methods: |
This should be an upstream PR first (GitHub.com/python/asyncio). Also, too late for 3.6. |
Also it's not clear that it's a good idea without more thought -- there's no way to end the loop on the producing side. |
I might be missing something, but can't something similar be said of queue.get()? |
That's my point, actually. If you are wrapping the Queue protocol with __aiter__/anext the caller would expect there's a way to signal to the latter that the loop is over (so it can raise StopAsyncIteration). But since .get() doesn't have a way to signal this, an async for-loop would not be able to terminate (other than through break). |
I successfully use my code: import asyncio, sanic
class MyQueue(asyncio.Queue):
def __aiter__(self): return self
async def __anext__(self): return await self.get()
app = sanic.Sanic()
ws_set = set()
app.static('/', 'async.html') @app.websocket('/ws') async def postgres():
import aiopg
async with aiopg.create_pool('') as pool:
async with pool.acquire() as connection:
connection._notifies = MyQueue()
async with connection.cursor() as cursor:
await cursor.execute('LISTEN message')
async for message in connection.notifies:
for ws in ws_set: await ws.send(message.payload) try: asyncio.get_event_loop().run_until_complete(asyncio.gather(app.create_server(), postgres())) |
So that's an infinite loop right? |
I attempted this myself, it seemed to have too many costs associated for the stdlib and is something easy enough to wrap myself when I need this functionality with explicit semantics on how to "stop" the queue (using an |
Let's not do this. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: