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

Documentation: Hello-world example using threading instead of asyncio #1437

Open
josephernest opened this issue Jan 30, 2024 · 1 comment
Open

Comments

@josephernest
Copy link

josephernest commented Jan 30, 2024

This doc page gives a hello-world example: https://websockets.readthedocs.io/en/stable/index.html using the asyncio paradigm.

It supports several network I/O and control flow paradigms:

The default implementation builds upon asyncio, Python’s standard asynchronous I/O framework. It provides an elegant coroutine-based API. It’s ideal for servers that handle many clients concurrently.

The threading implementation is a good alternative for clients, especially if you aren’t familiar with asyncio. It may also be used for servers that don’t need to serve many clients.
...

Is there somewhere in the official documentation a hello-world simple server example using just threading, without asyncio?

Thanks!

@aaugustin
Copy link
Member

#!/usr/bin/env python

from websockets.sync.server import serve

def hello(websocket):
    name = websocket.recv()
    print(f"< {name}")

    greeting = f"Hello {name}!"

    websocket.send(greeting)
    print(f"> {greeting}")

with serve(hello, "localhost", 8765) as server:
    server.serve_forever()

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

No branches or pull requests

2 participants