Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

running multiple websocket connections #547

Closed
sam-thecoder opened this issue Jan 4, 2019 · 3 comments
Closed

running multiple websocket connections #547

sam-thecoder opened this issue Jan 4, 2019 · 3 comments
Labels

Comments

@sam-thecoder
Copy link

Here's the snippet of code that I have

import asyncio
import websockets

async def get_cells(websocket, path):
    async for message in websocket:
        ... some code

async def get_shaded_area(websocket, path):
    async for message in websocket:
        ... #some code
        

asyncio.get_event_loop().run_until_complete(
    websockets.serve(get_cells, 'localhost', 8765))

asyncio.get_event_loop().run_until_complete(
    websockets.serve(get_shaded_area, 'localhost/shade-area', 8765))
    
asyncio.get_event_loop().run_forever()

it started with only get_cells so it only had one get_event_loop for get_cells at that point it was working ok.

Since I want to make more that one ws connection since each handles different data and returns different results the second get_shaded_area after adding it throws this error:

(pixelart) sam@sam-Lenovo-G51-35:~/code/pixelart$ python path.py
Traceback (most recent call last):
File "path.py", line 144, in <module>
    websockets.serve(get_shaded_area, 'localhost/shade-area', 8765))
File "/usr/lib/python3.6/asyncio/base_events.py", line 473, in run_until_complete
    return future.result()
File "/usr/lib/python3.6/asyncio/tasks.py", line 537, in _wrap_awaitable
    return (yield from awaitable.__await__())
File "/home/sam/code/envs/pixelart/lib/python3.6/site-packages/websockets/py35/server.py", line 13, in __await_impl__
    server = await self._creating_server
File "/usr/lib/python3.6/asyncio/base_events.py", line 1019, in create_server
    infos = yield from tasks.gather(*fs, loop=self)
File "/usr/lib/python3.6/asyncio/base_events.py", line 968, in _create_server_getaddrinfo
    flags=flags, loop=self)
File "/usr/lib/python3.6/concurrent/futures/thread.py", line 56, in run
    result = self.fn(*self.args, **self.kwargs)
File "/usr/lib/python3.6/socket.py", line 745, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known
@aaugustin
Copy link
Member

In order to handle differently different paths, you need to do the routing yourself (until #311 is fixed), like this:

async def get_cells(websocket):
    async for message in websocket:
        ...

async def get_shaded_area(websocket):
    async for message in websocket:
        ...

async def router(websocket, path):
    if path == "/":
        await get_cells(websocket)
    elif path == "/shade-area":
        await get_shaded_area(websocket)

asyncio.get_event_loop().run_until_complete(
    websockets.serve(router, 'localhost', 8765))

asyncio.get_event_loop().run_forever()

@sam-thecoder
Copy link
Author

works but there's a typo, both get_cells and `get_shaded_area should include the path like so

 get_cells(websocket, path)

@aaugustin
Copy link
Member

My example removed the second argument from their signatures instead, since it isn't used after routing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants