1

I have multiple servers, each server is instance returning by asyncio.start_server. I need my web_server to works with websockets, to have possibility getting data using my javascript client. As I can see, asyncio do not provide websockets, only tcp sockets. Maybe I missed something ? I want to implement websocket server that I can using in asyncio.gather like below:

    loop = asyncio.get_event_loop()

    login_server = LoginServer.create()
    world_server = WorldServer.create()
    web_server   = WebServer.create()

    loop.run_until_complete(
        asyncio.gather(
            login_server.get_instance(),
            world_server.get_instance(),
            web_server.get_instance()
        )
    )

    try:
        loop.run_forever()
    except KeyboardInterrupt:
        pass

    loop.close()

I do not want to use aiohttp cause if using like in code above aiohttp just blocks another tasks. I need something that will be non-blocking and that will have access to data of another servers (login and world). Does it possible with asyncio ? Does asyncio provide something like websockets ? How to implement websocket server for using in asyncio.gather ?

1 Answer 1

1

Well, finally I've implemented WebServer for using in another thread with asyncio. The code (WebServer code):

from aiohttp import web


class WebServer(BaseServer):

    def __init__(self, host, port):
        super().__init__(host, port)

    @staticmethod
    async def handle_connection(self, request: web.web_request):
        ws = web.WebSocketResponse()
        await ws.prepare(request)

        async for msg in ws:
            Logger.debug('[Web Server]: {}'.format(msg))

        return ws

    @staticmethod
    def run():
        app = web.Application()
        web.run_app(app, host=Connection.WEB_SERVER_HOST.value, port=Connection.WEB_SERVER_PORT.value)

And how to run:

executor = ProcessPoolExecutor()

loop.run_until_complete(
    asyncio.gather(
        login_server.get_instance(),
        world_server.get_instance(),
        loop.run_in_executor(executor, WebServer.run)
    )
)
Sign up to request clarification or add additional context in comments.

1 Comment

Running web.run_app with run_in_executor is an anti-pattern because it (unnecessarily) runs two event loops. You can use the AppRunner API instead, as shown in the first code snippet of this answer.

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.