mirror of
https://github.com/gradio-app/gradio.git
synced 2025-04-06 12:30:29 +08:00
Handle async funcs within sync scopes (#1547)
* handle_async_funcs - save temporary work * handle_async_funcs - handle async functions in sync domain both in background or sync * handle_async_funcs - tweaks * handle_async_funcs - format * handle_async_funcs - tweaks * handle_async_funcs - fixes * revert gradio.egg files * added to requires Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
This commit is contained in:
parent
26745a88dc
commit
04a0764394
@ -2,6 +2,7 @@ analytics-python
|
||||
aiohttp
|
||||
fastapi
|
||||
ffmpy
|
||||
fsspec
|
||||
markdown-it-py[linkify,plugins]
|
||||
matplotlib
|
||||
numpy
|
||||
|
@ -1,5 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import copy
|
||||
import getpass
|
||||
import inspect
|
||||
@ -375,9 +376,7 @@ class Blocks(BlockContext):
|
||||
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)
|
||||
|
||||
@ -745,7 +744,7 @@ class Blocks(BlockContext):
|
||||
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
|
||||
|
@ -16,6 +16,7 @@ from typing import TYPE_CHECKING, Any, Callable, Dict, List
|
||||
|
||||
import aiohttp
|
||||
import analytics
|
||||
import fsspec.asyn
|
||||
import pkg_resources
|
||||
import requests
|
||||
|
||||
@ -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))
|
||||
|
Loading…
x
Reference in New Issue
Block a user