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

Handle async funcs within sync scopes #1547

Merged
merged 10 commits into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gradio.egg-info/requires.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ analytics-python
aiohttp
fastapi
ffmpy
fsspec
markdown-it-py[linkify,plugins]
matplotlib
numpy
Expand Down
7 changes: 3 additions & 4 deletions gradio/blocks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import asyncio
import copy
import getpass
import inspect
Expand Down Expand Up @@ -375,9 +376,7 @@ def __call__(self, *params, fn_index=0):
processed_input = self.preprocess_data(fn_index, serialized_params, None)

if inspect.iscoroutinefunction(block_fn.fn):
raise ValueError(
"Cannot call Blocks object as a function if the function is a coroutine"
)
predictions = utils.synchronize_async(block_fn.fn, *processed_input)
else:
predictions = block_fn.fn(*processed_input)

Expand Down Expand Up @@ -745,7 +744,7 @@ def launch(
self.enable_queue = True
else:
self.enable_queue = enable_queue or False
utils.synchronize_async(self.create_limiter, max_threads)
utils.run_coro_in_background(self.create_limiter, max_threads)
self.config = self.get_config_file()
self.share = share
self.encrypt = encrypt
Expand Down
36 changes: 33 additions & 3 deletions gradio/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import aiohttp
import analytics
import fsspec.asyn
import pkg_resources
import requests

Expand Down Expand Up @@ -312,7 +313,36 @@ def component_or_layout_class(cls_name: str) -> Component | BlockContext:
raise ValueError(f"No such component or layout: {cls_name}")


def synchronize_async(func: Callable, *args: object, callback_func: Callable = None):
def synchronize_async(func: Callable, *args, **kwargs):
"""
Runs async functions in sync scopes.

Example:
if inspect.iscoroutinefunction(block_fn.fn):
predictions = utils.synchronize_async(block_fn.fn, *processed_input)

Args:
func:
*args:
**kwargs:
"""
return fsspec.asyn.sync(fsspec.asyn.get_loop(), func, *args, **kwargs)


def run_coro_in_background(func: Callable, *args, **kwargs):
"""
Runs coroutines in background.

Example:
utils.run_coro_in_background(fn, *args, **kwargs)

Args:
func:
*args:
**kwargs:

Returns:

"""
event_loop = asyncio.get_event_loop()
task = event_loop.create_task(func(*args))
task.add_done_callback(callback_func)
_ = event_loop.create_task(func(*args, **kwargs))
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"pydub",
"requests",
"uvicorn",
"Jinja2"
"Jinja2",
"fsspec",
],
entry_points={
'console_scripts': ['gradio=gradio.reload:run_in_reload_mode']
Expand Down