109 lines
No EOL
3.6 KiB
Python
Executable file
109 lines
No EOL
3.6 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
"""
|
|
Test script to demonstrate cross-process WebSocket emission issues.
|
|
This simulates what happens when AI processing runs in separate asyncio context.
|
|
"""
|
|
|
|
import os
|
|
import threading
|
|
import time
|
|
import asyncio
|
|
from app import create_app
|
|
from app.models.focus_group import emit_websocket_event
|
|
|
|
def simulate_main_process():
|
|
"""Simulate the main Flask/SocketIO process."""
|
|
print("=" * 50)
|
|
print("SIMULATING MAIN FLASK/SOCKETIO PROCESS")
|
|
print("=" * 50)
|
|
|
|
# Create Flask app (this initializes WebSocket manager)
|
|
app = create_app()
|
|
|
|
main_pid = os.getpid()
|
|
main_thread = threading.get_ident()
|
|
print(f"🔌 Main Process - PID: {main_pid}, Thread: {main_thread}")
|
|
|
|
# Simulate WebSocket connection handling
|
|
print("🔌 WebSocket connections would be handled in this process/thread")
|
|
|
|
return app
|
|
|
|
def simulate_ai_processing():
|
|
"""Simulate AI processing that emits WebSocket events."""
|
|
print("\n" + "=" * 50)
|
|
print("SIMULATING AI PROCESSING (SEPARATE CONTEXT)")
|
|
print("=" * 50)
|
|
|
|
ai_pid = os.getpid()
|
|
ai_thread = threading.get_ident()
|
|
print(f"🤖 AI Process - PID: {ai_pid}, Thread: {ai_thread}")
|
|
|
|
# Simulate emitting WebSocket events from AI processing
|
|
test_focus_group_id = "test_fg_123"
|
|
test_message_data = {
|
|
"id": "msg_456",
|
|
"content": "This is a test message from AI processing",
|
|
"sender_name": "AI Assistant",
|
|
"timestamp": time.time()
|
|
}
|
|
|
|
print(f"🤖 AI attempting to emit WebSocket event...")
|
|
emit_websocket_event('message_update', test_focus_group_id, test_message_data)
|
|
print(f"🤖 AI emit completed")
|
|
|
|
async def simulate_async_ai_processing():
|
|
"""Simulate asyncio-based AI processing (like AutonomousConversationController)."""
|
|
print("\n" + "=" * 50)
|
|
print("SIMULATING ASYNCIO AI PROCESSING")
|
|
print("=" * 50)
|
|
|
|
async_pid = os.getpid()
|
|
async_thread = threading.get_ident()
|
|
print(f"🔄 AsyncIO Process - PID: {async_pid}, Thread: {async_thread}")
|
|
|
|
# Simulate async AI processing with WebSocket events
|
|
test_focus_group_id = "test_fg_async_789"
|
|
test_message_data = {
|
|
"id": "msg_async_101",
|
|
"content": "This is a test message from asyncio AI processing",
|
|
"sender_name": "Async AI Assistant",
|
|
"timestamp": time.time()
|
|
}
|
|
|
|
print(f"🔄 AsyncIO AI attempting to emit WebSocket event...")
|
|
emit_websocket_event('message_update', test_focus_group_id, test_message_data)
|
|
print(f"🔄 AsyncIO AI emit completed")
|
|
|
|
# Simulate processing delay
|
|
await asyncio.sleep(0.1)
|
|
|
|
def main():
|
|
"""Main test function."""
|
|
print("🧪 WEBSOCKET CROSS-PROCESS TEST")
|
|
print("This test demonstrates potential cross-process WebSocket emission issues")
|
|
print("identified by GPT-5 analysis.\n")
|
|
|
|
# Step 1: Simulate main Flask/SocketIO process
|
|
app = simulate_main_process()
|
|
|
|
# Step 2: Simulate AI processing in same thread
|
|
simulate_ai_processing()
|
|
|
|
# Step 3: Simulate async AI processing
|
|
print("\n🔄 Running asyncio AI simulation...")
|
|
asyncio.run(simulate_async_ai_processing())
|
|
|
|
print("\n" + "=" * 50)
|
|
print("TEST ANALYSIS")
|
|
print("=" * 50)
|
|
print("👀 Key observations to look for:")
|
|
print("1. Are all PID/Thread IDs the same?")
|
|
print("2. If different, this confirms cross-process emission issue")
|
|
print("3. WebSocket events emitted from different process won't reach clients")
|
|
print("4. This explains why AI mode messages don't appear in real-time")
|
|
|
|
print("\n✅ Cross-process test completed")
|
|
|
|
if __name__ == "__main__":
|
|
main() |