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
29.49k stars 2.19k forks source link

Convert sse calls in client from async to sync #8182

Closed abidlabs closed 2 weeks ago

abidlabs commented 2 weeks ago

This converts all of the HTTP / SSE calls in the python client from async to sync. There was no benefit in keeping them async as we would convert them to synchronous calls using utils.synchronize_async anyways and the use of this method was preventing the client from being used in environments that have their own async event management like gunicorn/gevent.

Closes: https://github.com/gradio-app/gradio/issues/7581

To test, create a file called test.py with the following code:

from gevent import monkey
monkey.patch_all()
from gradio_client import Client
from flask import Flask

app = Flask(__name__)

client = Client("abidlabs/en2fr")  # a Space that translates from English to French

@app.route("/gen")
def gen():
      result = client.predict(
                  "hello",  
                  api_name="/predict"
              )
      return result

if __name__ == "__main__":
      app.run(host="0.0.0.0", port=5000)

Then run:

gunicorn -k gevent -w 1 test:app --bind 0.0.0.0:5000

Then visit http://localhost:5000/gen and you should see a valid greeting.

Note: I didn't change the ws calls since websocket.send/receive are async by nature and so we'd have to do a workaround like utils.synchronize_async anyways to support them. So if you use the Client to connect to Gradio apps running 3.x ("compatibility mode"), it will still use the async client.

gradio-pr-bot commented 2 weeks ago

🪼 branch checks and previews

• Name Status URL
Spaces ready! Spaces preview
Website ready! Website preview
:unicorn: Changes detected! Details

Install Gradio from this PR

pip install https://gradio-builds.s3.amazonaws.com/9cd5a091da9a9fd09d639e8bc1f3288454d2a44b/gradio-4.28.3-py3-none-any.whl

Install Gradio Python Client from this PR

pip install "gradio-client @ git+https://github.com/gradio-app/gradio@9cd5a091da9a9fd09d639e8bc1f3288454d2a44b#subdirectory=client/python"
gradio-pr-bot commented 2 weeks ago

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
gradio patch
gradio_client patch

With the following changelog entry.

Convert sse calls in client from async to sync

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](https://github.com/gradio-app/gradio/edit/sync-client/.changeset/great-poets-visit.md).
abidlabs commented 2 weeks ago

This looks good to me @abidlabs! Tested some local demos and everything looks good. One comment I have is whether we can stop raising CancelledError and just return now? That should stop the job in the threadpool no?

Yup I think you're right

abidlabs commented 2 weeks ago

One comment I have is whether we can stop raising CancelledError and just return now? That should stop the job in the threadpool no?

Ok so I spent some time on this, but if we stop raising CancelledError and instead return None, then several upstream methods fail. The basic problem is we need to decide what job.result() should produce if the job is canceled. I think its not a bad idea for it to just raise a CancelledError as is currently the behavior? We could potentially change this and I'm open to other ideas, but I think we can make that part of another PR, perhaps as we address: https://github.com/gradio-app/gradio/issues/8188

abidlabs commented 2 weeks ago

I'll go ahead and merge this in but open to suggestions on this @freddyaboulton

abidlabs commented 2 weeks ago

Thanks for the reviews @aliabid94 and @freddyaboulton!

kxbin commented 1 week ago

great work :+1: