gradio-app / gradio

Build and share delightful machine learning apps, all in Python. 🌟 Star to support our work!
http://www.gradio.app
Apache License 2.0
33.42k stars 2.53k forks source link

gradio cc dev one CPU core full usage #7095

Closed velaia closed 9 months ago

velaia commented 9 months ago

Describe the bug

I've noticed my MacBook battery draining rapidly while working on a keypoint detection component for gradio. I noticed that there's a Python process that is at 100%.

Under the project issues I found #7086 and #6847 and not understanding the code + project enough I checked whether upgrade to 4.15.0 or addition of the time.sleep(0.001) from #7086 in the corresponding client.py would fix my issue but it didn't.

Under the site-packages I searched for "while True" and had a quick shot at the one occurrence under gradio/cli/commands/components/dev.py but found this was not related.

Maybe someone with more understanding of the code can reproduce or give me a tip on where to look.

I've also found that I can kill the 100% Python process in the Activity Monitor and the "gradio cc dev" will still run.

Have you searched existing issues? πŸ”Ž

Reproduction

When starting to develop a new component. Can easily be reproduced

gradio cc create testcomponent --template Image
cd testcomponent
gradio cc dev

Screenshot

No response

Logs

No response

System Info

macOs: 14.1.1 (23B81)
Python 3.12.1
gradio                    4.15.0
gradio_client             0.8.1

Severity

I can work around it

velaia commented 9 months ago

I've checked the process ID and started it using the profiler

python -m cProfile -o prof1.prof /Users/user/git/gradio_dev/.venv/bin/gradio demo/app.py --watch-dirs /Users/daniel/git/gradio_dev/testcomponent

The prof1.prof (renamed to txt so I could upload it) is attached to this comment. prof1.txt

velaia commented 9 months ago

So the profiling file is huge (1.8 MB), even if I only run the program for a couple of seconds. The snakeviz tool to visualise the profiling session hits a limit at a depth of 15 and I have to set the cutoff to 1/100 to even get there. It seems to me that there's an issue with recursion.

Screenshot 2024-01-22 at 09 39 44

At the same time I've found that the issue occurs even if I run it on the main directory of my project, not the custom-component subdirectory. So it seemingly doesn't have anything to do with the custom-components code. The gradio interface in the app.py file is from the (beginner) tutorial I believe:

import gradio as gr

def greet(name, intensity):
    return "Hello " * intensity + name + "!"

demo = gr.Interface(
    fn=greet,
    inputs=['text', 'slider'],
    outputs=['text'],
)

demo.launch()
velaia commented 9 months ago

After some more digging I've found that the issue seems related to the process that's being used to start the app: If I don't use the "gradio" mechanism (including the subprocess.Popen and popen.wait as well as the watch_dirs) but start the app.py directly using the following command, the CPU usage stays below 1%.

python -u app.py