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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use async iteration for progressive outputs #2409

Merged
merged 6 commits into from Oct 7, 2022
Merged

Conversation

freddyaboulton
Copy link
Collaborator

@freddyaboulton freddyaboulton commented Oct 7, 2022

Description

I noticed that if two different users are running an iterative output demo at the same time, the two demos will not iterate asynchronously. When one demo is iterating, the other is not. This increases the latency of serving outputs to users.

You can see this here:

Main

iterators_blocking_each_other

This branch

iterators_not_blocking_each_other

The culprit is that next(iterator) is not async, so it blocks the event loop for all users.

This is the demo code to test:

import time
import gradio as gr
import random

def fake_diffusion(steps):
    for i in range(steps):
        time.sleep(1)
        yield random.choice([
            "https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=387&q=80",
            "https://images.unsplash.com/photo-1554151228-14d9def656e4?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=386&q=80",
            "https://images.unsplash.com/photo-1542909168-82c3e7fdca5c?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MXx8aHVtYW4lMjBmYWNlfGVufDB8fDB8fA%3D%3D&w=1000&q=80",
            "https://images.unsplash.com/photo-1546456073-92b9f0a8d413?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=387&q=80",
            "https://images.unsplash.com/photo-1601412436009-d964bd02edbc?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=464&q=80",
            ])

with gr.Blocks() as demo:
    n = gr.Slider(1, 10, value=3, step=1)
    output = gr.Image().style(height=250, width=250)
    run = gr.Button()
    run.click(fake_diffusion, n, output)

demo.queue(concurrency_count=20, max_size=20).launch()

Checklist:

  • I have performed a self-review of my own code
  • I have added a short summary of my change to the CHANGELOG.md
  • My code follows the style guidelines of this project
  • I have commented my code in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

A note about the CHANGELOG

Hello 馃憢 and thank you for contributing to Gradio!

All pull requests must update the change log located in CHANGELOG.md, unless the pull request is labeled with the "no-changelog-update" label.

Please add a brief summary of the change to the Upcoming Release > Full Changelog section of the CHANGELOG.md file and include
a link to the PR (formatted in markdown) and a link to your github profile (if you like). For example, "* Added a cool new feature by [@myusername](link-to-your-github-profile) in [PR 11111](https://github.com/gradio-app/gradio/pull/11111)".

If you would like to elaborate on your change further, feel free to include a longer explanation in the other sections.
If you would like an image/gif/video showcasing your feature, it may be best to edit the CHANGELOG file using the
GitHub web UI since that lets you upload files directly via drag-and-drop.

@github-actions
Copy link
Contributor

github-actions bot commented Oct 7, 2022

All the demos for this PR have been deployed at https://huggingface.co/spaces/gradio-pr-deploys/pr-2409-all-demos

@freddyaboulton freddyaboulton force-pushed the use-async-iteration branch 2 times, most recently from 39e77d2 to 7db6ea4 Compare October 7, 2022 14:39
@freddyaboulton freddyaboulton marked this pull request as ready for review October 7, 2022 14:42
@abidlabs
Copy link
Member

abidlabs commented Oct 7, 2022

Great catch @freddyaboulton! Will review shortly

try:
return next(iterator)
except StopIteration:
# raise a ValueError here because co-routines can't raise StopIteration themselves
Copy link
Member

Choose a reason for hiding this comment

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

@abidlabs
Copy link
Member

abidlabs commented Oct 7, 2022

LGTM @freddyaboulton reproduced the original issue and confirmed that this fixes it. Just a comment above in case we can avoid defining a new exception.

@freddyaboulton
Copy link
Collaborator Author

Thanks for the review and suggestion @abidlabs ! StopAsyncIteration works!

@freddyaboulton freddyaboulton merged commit da93f4f into main Oct 7, 2022
@freddyaboulton freddyaboulton deleted the use-async-iteration branch October 7, 2022 21:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants