Skip to content

connection keepalive is not really async #872

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

Closed
tgulyas-sps opened this issue Dec 14, 2020 · 5 comments
Closed

connection keepalive is not really async #872

tgulyas-sps opened this issue Dec 14, 2020 · 5 comments

Comments

@tgulyas-sps
Copy link

related #581

I've starting using this library and at the moment I have an endless loop in "async def client_connected(self, websocket, path): " to keep the connection alive.

However this is not a real soltion just a workaround to a problem, the moment you add endless loop the asyncio user is not able to use any other async tasks as this function will endlessly block the asyncio loop execution.

Wouldn't be a full asyncio solution to have a different API where functions are callbacks and first argument expresses what client triggered the event?
"client_connected(ws...)"
"data_received(ws, data)
"cliend_disconnected(ws, ...)"
...
etc

@aaugustin
Copy link
Member

No, a callback-based API wouldn't feel native in asyncio.

You're doing something that blocks the event loop, but I cannot understand why with the limited amount of details you provided.

https://github.com/aaugustin/websockets/blob/master/example/show_time.py is an example of a server that does exactly what you say and doesn't block the event loop. You can connect at least 10k clients to this server.

@aaugustin
Copy link
Member

Also, unfortunately, the issue you're linking to was a hodgepodge of unrelated tickets. It's more likely to create confusion than clarity.

@tgulyas-sps
Copy link
Author

So this is a non-blocking call?

while True: data = await websocket.recv();

@aaugustin
Copy link
Member

It's not blocking.

@aaugustin
Copy link
Member

You can also use:

async for data in websocket:
   ...

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

No branches or pull requests

2 participants