HTTPServer is single-threaded, so the wallpaper's persistent SSE connection blocked all subsequent requests. ThreadingHTTPServer handles each connection in its own thread. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
45 lines
1.3 KiB
Python
45 lines
1.3 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Reads CAVA ASCII output from stdin and streams it to the wallpaper
|
|
via Server-Sent Events on http://127.0.0.1:5555.
|
|
Run as: cava -p cava.conf | python3 cava-server.py
|
|
"""
|
|
import sys
|
|
import threading
|
|
import time
|
|
from http.server import ThreadingHTTPServer, BaseHTTPRequestHandler
|
|
|
|
latest = ""
|
|
lock = threading.Lock()
|
|
|
|
class Handler(BaseHTTPRequestHandler):
|
|
def do_GET(self):
|
|
self.send_response(200)
|
|
self.send_header("Content-Type", "text/event-stream")
|
|
self.send_header("Cache-Control", "no-cache")
|
|
self.send_header("Access-Control-Allow-Origin", "*")
|
|
self.end_headers()
|
|
last = None
|
|
try:
|
|
while True:
|
|
with lock:
|
|
cur = latest
|
|
if cur and cur != last:
|
|
self.wfile.write(f"data: {cur}\n\n".encode())
|
|
self.wfile.flush()
|
|
last = cur
|
|
time.sleep(0.016) # cap at ~60fps
|
|
except Exception:
|
|
pass
|
|
|
|
def log_message(self, *args):
|
|
pass
|
|
|
|
def serve():
|
|
ThreadingHTTPServer(("127.0.0.1", 5555), Handler).serve_forever()
|
|
|
|
threading.Thread(target=serve, daemon=True).start()
|
|
|
|
for line in sys.stdin:
|
|
with lock:
|
|
latest = line.strip()
|