I have a class that serves web sockets, and listen to PostgreSQL. Using asyncpg, when I try to uses add_listener, i get the error: RuntimeWarning: coroutine was never awaited. How to async / await a callback. I tried adding "await self.listener" but it doesn't work.
Is there a way to handle this in another way?
import asyncio
import http
import websockets
import asyncpg
class App(object):
def __init__(self, loop):
self.loop = loop
self.ws_list = []
self.conn = None
async def ws_handler(self, ws, path):
if self.conn is None:
self.conn = await asyncpg.connect(user='xxx', password='xxx', database='pgws', host='127.0.0.1')
await self.conn.add_listener('todo_updates', self.listener)
print('new socket!!!')
self.ws_list.append(ws)
while True:
await asyncio.sleep(1)
async def listener(self, conn, pid, channel, payload):
print(payload)
for ws in self.ws_list:
task = asyncio.create_task()
await ws.send(payload)
if __name__ == "__main__":
loop = asyncio.get_event_loop()
app = App(loop)
start_server = websockets.serve(app.ws_handler, 'localhost', 8766)
app.loop.run_until_complete(start_server)
app.loop.run_forever()