3

I'm writing an application that needs to take in events from two sources. Some events will come from a Websocket client connection, and some will come from incoming HTTP requests. Is it possible with aiohttp to have both of these listeners in the same application, or do I need two separate executables?

I currently have a function that takes websocket events, and calls a callback on each event

async def callback(msg):
    print(msg)

async def websocket():
    session = aiohttp.ClientSession()
    async with session.ws_connect('http://example.org/websocket') as ws:

        async for msg in ws:
            if msg.type == aiohttp.WSMsgType.TEXT:
                await callback(msg.data)
            elif msg.type == aiohttp.WSMsgType.CLOSED:
                break
            elif msg.type == aiohttp.WSMsgType.ERROR:
                break

The problem is that this blocks forever, so I don't know how I could integrate this with the aiohttp server. If I do:

await websocket()
aiohttp.web.run_app(app)

then web.run_app is never called. Is there some way to accomplish this? It seems like the ideal case for asyncio, multiple things handling multiple events asynchronously. How do I do it?

1 Answer 1

4

I was able to accomplish my goals by using the on_startup handler of my aiohttp app.

async def callback(msg):
    print(msg)

async def websocket(session):
    async with session.ws_connect('http://example.org/websocket') as ws:
        async for msg in ws:
            if msg.type == aiohttp.WSMsgType.TEXT:
                await callback(msg.data)
            elif msg.type == aiohttp.WSMsgType.CLOSED:
                break
            elif msg.type == aiohttp.WSMsgType.ERROR:
                break

async def init(app):
    session = aiohttp.ClientSession()
    app['websocket_task'] = app.loop.create_task(websocket(session))

app = web.Application()
app.on_startup.append(init)
Sign up to request clarification or add additional context in comments.

2 Comments

Canonical solution
You should also consider closing the session using on_shutdown

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.