Skip to content

Commit 07dc564

Browse files
committed
Add asyncio & threading examples to the homepage.
Fix #1437.
1 parent 524dd4a commit 07dc564

File tree

6 files changed

+69
-10
lines changed

6 files changed

+69
-10
lines changed

docs/index.rst

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,24 @@ list of features provided by each implementation.
6060
implementation, and then some. If you're using the historical implementation,
6161
you should :doc:`ugrade to the new implementation <howto/upgrade>`.
6262

63-
Here's an echo server using the :mod:`asyncio` API:
63+
Here's an echo server and corresponding client.
6464

65-
.. literalinclude:: ../example/echo.py
65+
.. tab:: asyncio
6666

67-
Here's a client using the :mod:`threading` API:
67+
.. literalinclude:: ../example/asyncio/echo.py
6868

69-
.. literalinclude:: ../example/hello.py
69+
.. tab:: threading
70+
71+
.. literalinclude:: ../example/sync/echo.py
72+
73+
.. tab:: asyncio
74+
:new-set:
75+
76+
.. literalinclude:: ../example/asyncio/hello.py
77+
78+
.. tab:: threading
79+
80+
.. literalinclude:: ../example/sync/hello.py
7081

7182
Don't worry about the opening and closing handshakes, pings and pongs, or any
7283
other behavior described in the WebSocket specification. websockets takes care
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
#!/usr/bin/env python
22

3+
"""Echo server using the asyncio API."""
4+
35
import asyncio
46
from websockets.asyncio.server import serve
57

8+
69
async def echo(websocket):
710
async for message in websocket:
811
await websocket.send(message)
912

13+
1014
async def main():
11-
async with serve(echo, "localhost", 8765):
12-
await asyncio.get_running_loop().create_future() # run forever
15+
async with serve(echo, "localhost", 8765) as server:
16+
await server.serve_forever()
17+
1318

14-
asyncio.run(main())
19+
if __name__ == "__main__":
20+
asyncio.run(main())

example/asyncio/hello.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env python
2+
3+
"""Client using the asyncio API."""
4+
5+
import asyncio
6+
from websockets.asyncio.client import connect
7+
8+
9+
async def hello():
10+
async with connect("ws://localhost:8765") as websocket:
11+
await websocket.send("Hello world!")
12+
message = await websocket.recv()
13+
print(message)
14+
15+
16+
if __name__ == "__main__":
17+
asyncio.run(hello())

example/ruff.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[lint.isort]
2+
no-sections = true

example/sync/echo.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env python
2+
3+
"""Echo server using the threading API."""
4+
5+
from websockets.sync.server import serve
6+
7+
8+
def echo(websocket):
9+
for message in websocket:
10+
websocket.send(message)
11+
12+
13+
def main():
14+
with serve(echo, "localhost", 8765) as server:
15+
server.serve_forever()
16+
17+
18+
if __name__ == "__main__":
19+
main()
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
#!/usr/bin/env python
22

3-
import asyncio
3+
"""Client using the threading API."""
4+
45
from websockets.sync.client import connect
56

7+
68
def hello():
79
with connect("ws://localhost:8765") as websocket:
810
websocket.send("Hello world!")
911
message = websocket.recv()
10-
print(f"Received: {message}")
12+
print(message)
13+
1114

12-
hello()
15+
if __name__ == "__main__":
16+
hello()

0 commit comments

Comments
 (0)