Example: Sync NumPy¶
A broadcast variant of the NumPy stream where every viewer shares the same frame feed. Demonstrates coordinated fan-out with background tasks.
Location¶
- Repository:
https://github.com/cetic/pyplet_examples - Path:
apps/pyplet_examples/examples/sync_numpy_* - Client entry point:
sync_numpy_client.py - Server loop:
sync_numpy_server.py
Fetch the examples submodule:
Highlights¶
sync_numpy_client.py:1reuses the standard NumPy client loop to stay aligned with the shared feed.
sync_numpy_server.py:17spawns a background task that keeps generating and broadcasting frames.
async def send_to_all():
while True:
if sockets:
bytes = io.BytesIO()
array = np.random.randn(10, 10)
np.save(bytes, array)
value = bytes.getvalue()
for ws in list(sockets):
try:
await ws.send(value)
except tornado.websocket.WebSocketClosedError:
sockets.remove(ws)
await asyncio.sleep(1)
asyncio.create_task(send_to_all())
sync_numpy_server.py:24prunes disconnected sockets while iterating the subscriber set.
for ws in list(sockets):
try:
await ws.send(value)
except tornado.websocket.WebSocketClosedError:
sockets.remove(ws)
Start here when you need a baseline for multi-client synchronized streams.