|
|
@@ -95,14 +95,28 @@ def erp_task_stream(request):
|
|
|
客户端通过 EventSource 连接此端点
|
|
|
|
|
|
注意:EventSource 会自动发送 cookies,所以认证应该可以正常工作
|
|
|
+ 重要:在生成器外部获取用户信息,避免在流处理期间访问会话
|
|
|
"""
|
|
|
+ # 在生成器外部获取用户信息,避免在流处理期间访问会话导致会话解码错误
|
|
|
+ try:
|
|
|
+ if hasattr(request, 'user') and request.user.is_authenticated:
|
|
|
+ user_id = getattr(request.user, 'id', None)
|
|
|
+ else:
|
|
|
+ user_id = 'anonymous'
|
|
|
+ except Exception as e:
|
|
|
+ # 如果访问用户信息时出错(可能因为会话损坏),使用匿名用户
|
|
|
+ logger.warning(f"获取用户信息失败,使用匿名用户: {str(e)}")
|
|
|
+ user_id = 'anonymous'
|
|
|
+
|
|
|
+ client_id = f"{user_id}_{int(time.time())}"
|
|
|
+ logger.info(f"SSE 连接建立: {client_id}")
|
|
|
+
|
|
|
def event_stream():
|
|
|
- # 生成客户端 ID
|
|
|
- user_id = getattr(request.user, 'id', None) if hasattr(request, 'user') and request.user.is_authenticated else 'anonymous'
|
|
|
- client_id = f"{user_id}_{int(time.time())}"
|
|
|
-
|
|
|
- logger.info(f"SSE 连接建立: {client_id}")
|
|
|
-
|
|
|
+ """
|
|
|
+ 流式响应生成器
|
|
|
+ 注意:在生成器内部不要访问 request.session 或 request.user
|
|
|
+ 这些信息已在生成器外部获取,避免会话解码错误
|
|
|
+ """
|
|
|
# 注册连接(检查连接数限制)
|
|
|
with connection_lock:
|
|
|
if len(connections) >= MAX_CONNECTIONS:
|