[issue28777] Add asyncio.Queue __aiter__, __anext__ methods

Georgy report at bugs.python.org
Thu Aug 3 03:47:41 EDT 2017


Georgy added the comment:

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 root_ws(request, ws):
    ws_set.add(ws)
    try:
        while True: await ws.recv()
    finally: ws_set.remove(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()))
except KeyboardInterrupt: asyncio.get_event_loop().stop()

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue28777>
_______________________________________


More information about the Python-bugs-list mailing list