zauberzeug / nicegui

Create web-based user interfaces with Python. The nice way.
https://nicegui.io
MIT License
8.78k stars 534 forks source link

connection time out when websocket saved large image from backend #3591

Closed iGoinbl closed 6 days ago

iGoinbl commented 2 weeks ago

Description

websocket connection time out :

i wanted to ask something about my app first of here it is a snapshot of it : everything is working great now except for one thing i am connected to comfyui as a backend with websockets ok ? when the generated image is large 4k or larger it doesn't return anything and the app gets time out message and reloads the websockets connection says 1000 sometimes more ms i already optimised the upload with aiofiles i convert all the images to web p with max size 2048 pixels max for viewing into the ui i think this problem happens when the webscoket is saving the image from comfyui since it taking long the ui freezes it is not when the ui is trying to load the webp its when the image generation is done and its saving this problem is not happening with 2k images do you guys have any idea any help would be great image

async def get_images(prompt):
    try:
        logging.info("Attempting WebSocket connection...")
        async with websockets.connect(
            f"ws://{server_address}/ws?clientId={client_id}", 
            max_size=1024 * 1024 * 100, 
            ping_interval=0.5, 
            ping_timeout=None,
            close_timeout=600  # 10 minutes timeout for closing the connection
        ) as ws:
            logging.info("WebSocket connection established.")

            async with aiohttp.ClientSession() as session:
                logging.info("Queueing prompt...")
                prompt_id = (await queue_prompt(session, prompt))['prompt_id']
                logging.info(f"Prompt ID: {prompt_id}")

                output_images = {}

                max_progress_steps = 20
                progress_step = 1 / max_progress_steps

                while True:
                    out = await ws.recv()
                    logging.info(f"Received WebSocket message: {out[:200]}")

                    if isinstance(out, str):
                        message = json.loads(out)
                        if message['type'] == 'executing':
                            data = message['data']
                            if data['node'] is None and data['prompt_id'] == prompt_id:
                                logging.info("Execution complete.")
                                break

                    else:
                        logging.info("Received non-string message, likely binary data.")
                        continue

                history = (await get_history(session, prompt_id))[prompt_id]
                for node_id, node_output in history['outputs'].items():
                    if 'images' in node_output:
                        images_output = []
                        for image in node_output['images']:
                            logging.info(f"Fetching image: {image['filename']}")
                            image_data = await get_image(session, image['filename'], image['subfolder'], image['type'])
                            images_output.append(image_data)
                        output_images[node_id] = images_output

            logging.info("All images fetched successfully.")
            return output_images

    except websockets.exceptions.ConnectionClosed as e:
        logging.error(f"WebSocket connection closed unexpectedly: {e}")
        return None
    except Exception as e:
        logging.error(f"An error occurred: {e}")
        return None
falkoschindler commented 2 weeks ago

It's hard to tell what your code is actually doing and when this function is called. Why is it important to provide a minimal reproducible example?

rodja commented 2 weeks ago

Is the image loaded via websocket? Maybe there is a max_buffer_size which block big data packages like we see in #3410.