Skip to content

Commit 19d4ee6

Browse files
aliabid94Ali Abidgradio-pr-botabidlabs
authoredNov 28, 2024··
Allow concurrent renders (#10059)
* changes * changes * changes * add changeset * changes * changes * changes * changes * Update gradio/blocks.py Co-authored-by: Abubakar Abid <abubakar@huggingface.co> --------- Co-authored-by: Ali Abid <aliabid94@gmail.com> Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com> Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
1 parent 47073ff commit 19d4ee6

File tree

5 files changed

+40
-1
lines changed

5 files changed

+40
-1
lines changed
 

‎.changeset/icy-moments-rescue.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"gradio": patch
3+
---
4+
5+
fix:Allow concurrent renders
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: render_heavy_concurrently"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio "]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "import random\n", "\n", "with gr.Blocks() as demo:\n", "\n", " with gr.Row():\n", "\n", " @gr.render()\n", " def render():\n", " for _ in range(500):\n", " gr.Textbox(str(random.randint(0, 100)))\n", " gr.Button(\"DONE 1\")\n", "\n", " @gr.render()\n", " def render2():\n", " for _ in range(500):\n", " gr.Textbox(str(random.randint(0, 100)))\n", " gr.Button(\"DONE 2\")\n", "\n", "if __name__ == \"__main__\":\n", " demo.queue(default_concurrency_limit=64).launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}

‎demo/render_heavy_concurrently/run.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import gradio as gr
2+
import random
3+
4+
with gr.Blocks() as demo:
5+
6+
with gr.Row():
7+
8+
@gr.render()
9+
def render():
10+
for _ in range(500):
11+
gr.Textbox(str(random.randint(0, 100)))
12+
gr.Button("DONE 1")
13+
14+
@gr.render()
15+
def render2():
16+
for _ in range(500):
17+
gr.Textbox(str(random.randint(0, 100)))
18+
gr.Button("DONE 2")
19+
20+
if __name__ == "__main__":
21+
demo.queue(default_concurrency_limit=64).launch()

‎gradio/blocks.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,10 @@ def get_layout(block: Block):
896896
config["layout"] = get_layout(root_block)
897897

898898
config["components"] = []
899-
for _id, block in self.blocks.items():
899+
blocks_items = list(
900+
self.blocks.items()
901+
) # freeze as list to prevent concurrent re-renders from changing the dict during loop, see https://github.com/gradio-app/gradio/issues/9991
902+
for _id, block in blocks_items:
900903
if renderable:
901904
if _id not in rendered_ids:
902905
continue
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { test, expect } from "@self/tootils";
2+
3+
test("1000 total textboxes render", async ({ page }) => {
4+
await page.getByText("DONE 1", { exact: false }).click();
5+
await page.getByText("DONE 2", { exact: false }).click();
6+
7+
const textboxes = await page.getByLabel("Textbox").all();
8+
expect(textboxes).toHaveLength(1000);
9+
});

0 commit comments

Comments
 (0)
Please sign in to comment.