Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use asyncio.Event to stop stream in heartbeat route #7932

Merged
merged 11 commits into from Apr 5, 2024

Conversation

freddyaboulton
Copy link
Collaborator

Description

This may fix some of the slowdowns we've seen in spaces loading recently.

🎯 PRs Should Target Issues

Before your create a PR, please check to see if there is an existing issue for this change. If not, please create an issue before you create this PR, unless the fix is very small.

Not adhering to this guideline will result in the PR being closed.

Tests

  1. PRs will only be merged if tests pass on CI. To run the tests locally, please set up your Gradio environment locally and run the tests: bash scripts/run_all_tests.sh

  2. You may need to run the linters: bash scripts/format_backend.sh and bash scripts/format_frontend.sh

@freddyaboulton freddyaboulton added the v: patch A change that requires a patch release label Apr 4, 2024
@gradio-pr-bot
Copy link
Contributor

gradio-pr-bot commented Apr 4, 2024

🪼 branch checks and previews

Name Status URL
Spaces ready! Spaces preview
Website ready! Website preview
Storybook ready! Storybook preview
🦄 Changes detected! Details

Install Gradio from this PR

pip install https://gradio-builds.s3.amazonaws.com/4751e5e682d2cd34cbde0f35dcb8e1032831a3a2/gradio-4.25.0-py3-none-any.whl

Install Gradio Python Client from this PR

pip install "gradio-client @ git+https://github.com/gradio-app/gradio@4751e5e682d2cd34cbde0f35dcb8e1032831a3a2#subdirectory=client/python"

@gradio-pr-bot
Copy link
Contributor

gradio-pr-bot commented Apr 4, 2024

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
gradio patch
  • Maintainers can select this checkbox to manually select packages to update.

With the following changelog entry.

Use asyncio.Event to stop stream in heartbeat route

Maintainers or the PR author can modify the PR title to modify this entry.

Something isn't right?

  • Maintainers can change the version label to modify the version bump.
  • If the bot has failed to detect any changes, or if this pull request needs to update multiple packages to different versions or requires a more comprehensive changelog entry, maintainers can update the changelog file directly.

@freddyaboulton freddyaboulton marked this pull request as ready for review April 4, 2024 17:42
@abidlabs
Copy link
Member

abidlabs commented Apr 4, 2024

Can you describe what this PR is doing conceptually @freddyaboulton? Not sure I understand it

gradio/routes.py Outdated
Comment on lines 610 to 612
# For some reason tests are failing when using events only on CI
# so we use a different approach for CI but
# locally and in deployment we should use the event-based approach
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slightly concerned about this. What's true on the CI runners may be true in some of users' deployments

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Do you know why it doesn;t work on CI? Ideally we use the event approach everywhere

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking into it. It may be a 3.8 issue.

@freddyaboulton
Copy link
Collaborator Author

We need to stop the heartbeat messages when the server is shutting down because starlette waits for all connections to close before exiting.

The old way we were doing that was by polling the is_running prop but that was taking up too much of the event loop. Some gradio spaces like gradio/calculator were taking a really long time to load (30 seconds).

So we're switching to this event-based approach that works better than polling.

@abidlabs
Copy link
Member

abidlabs commented Apr 4, 2024

Is the PR Deploy Space building for you @freddyaboulton? I'm seeing errors even after factory restarting

@aliabid94
Copy link
Collaborator

Nice PR, the event approach is cleaner but I don't think this should be merged until we know why it doesn't work on E2E tests.

@freddyaboulton
Copy link
Collaborator Author

@abidlabs The deployed space is up!

gradio/routes.py Outdated
# For some reason tests are failing when using events only on CI
# so we use a different approach for CI but
# locally and in deployment we should use the event-based approach
if os.getenv("GRADIO_IS_E2E_TEST") and sys.version_info < (3, 9):
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what's happening with CI but it's failing when using the "else" branch here. This makes it so that locally we use the "else" branch and on CI we use the other branch.

Tested both locally and they work.

gradio/routes.py Outdated
Comment on lines 610 to 612
# For some reason tests are failing when using events only on CI
# so we use a different approach for CI but
# locally and in deployment we should use the event-based approach
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking into it. It may be a 3.8 issue.

@freddyaboulton
Copy link
Collaborator Author

CI is finally green @abidlabs @aliabid94 ! It was a python 3.8 issue

Copy link
Collaborator

@aliabid94 aliabid94 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@freddyaboulton
Copy link
Collaborator Author

Thanks for the reviews everyone!!

@freddyaboulton freddyaboulton merged commit b78129d into main Apr 5, 2024
8 checks passed
@freddyaboulton freddyaboulton deleted the use-event-to-stop branch April 5, 2024 18:57
@pngwn pngwn mentioned this pull request Apr 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
v: patch A change that requires a patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants