This commit is contained in:
@@ -7,10 +7,10 @@ from time import sleep_ms
|
||||
# ----------------------------打印机引脚配置-------------------------------------------------
|
||||
|
||||
# TTL 引脚配置
|
||||
ttl_tx = Pin(2) # TTL TX 连接到引脚22
|
||||
ttl_rx = Pin(1) # TTL RX 连接到引脚23
|
||||
ttl_tx = Pin(18) # TTL TX 连接到引脚22
|
||||
ttl_rx = Pin(17) # TTL RX 连接到引脚23
|
||||
|
||||
ttl_Dtr = Pin(6) # TTL TX 连接到引脚22
|
||||
ttl_Dtr = Pin(12) # TTL TX 连接到引脚22
|
||||
|
||||
# ----------------------------epaper配置-------------------------------------------------
|
||||
|
||||
|
||||
@@ -427,6 +427,39 @@ async def handle_font_request(websocket, message_type, data):
|
||||
except Exception as e:
|
||||
print(f"Error handling font request: {e}")
|
||||
|
||||
class LockedWebSocket:
|
||||
"""
|
||||
WebSocket wrapper with a lock to prevent concurrent write operations.
|
||||
The websockets library (legacy protocol) does not support concurrent writes,
|
||||
which can lead to 'AssertionError: assert waiter is None' when multiple tasks
|
||||
try to send data simultaneously.
|
||||
"""
|
||||
def __init__(self, websocket: WebSocket):
|
||||
self.websocket = websocket
|
||||
self.lock = asyncio.Lock()
|
||||
|
||||
async def accept(self):
|
||||
await self.websocket.accept()
|
||||
|
||||
async def receive(self):
|
||||
return await self.websocket.receive()
|
||||
|
||||
async def send_text(self, data: str):
|
||||
async with self.lock:
|
||||
await self.websocket.send_text(data)
|
||||
|
||||
async def send_bytes(self, data: bytes):
|
||||
async with self.lock:
|
||||
await self.websocket.send_bytes(data)
|
||||
|
||||
async def send_json(self, data):
|
||||
async with self.lock:
|
||||
await self.websocket.send_json(data)
|
||||
|
||||
async def close(self, code=1000):
|
||||
async with self.lock:
|
||||
await self.websocket.close(code)
|
||||
|
||||
class MyRecognitionCallback(RecognitionCallback):
|
||||
def __init__(self, websocket: WebSocket, loop: asyncio.AbstractEventLoop):
|
||||
self.websocket = websocket
|
||||
@@ -758,6 +791,8 @@ def generate_image(prompt, progress_callback=None, retry_count=0, max_retries=2)
|
||||
|
||||
@app.websocket("/ws/audio")
|
||||
async def websocket_endpoint(websocket: WebSocket):
|
||||
# 使用 LockedWebSocket 包装原始 websocket 以防止并发写入冲突
|
||||
websocket = LockedWebSocket(websocket)
|
||||
global audio_buffer
|
||||
await websocket.accept()
|
||||
print("Client connected")
|
||||
|
||||
Reference in New Issue
Block a user