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 <rep...@bugs.python.org>
<http://bugs.python.org/issue28777>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to