Gradio image app: configure a shared queue and check concurrency
Share one queue across three image event listeners with concurrency_id, cap it with concurrency_limit=2, and check concurrency using editorial steps.
Task and deliverable · Prepare the input and environment · Mechanism and data flow
At a glance
When one Gradio page has several event listeners that trigger heavy image generation, you need to say how many queues they use and how many requests can run at once. This article relies only on the official Queuing page. It first covers how the default queue is split by event listener, then how concurrency_limit and concurrency_id change that split, and finally gives an editor-recommended check and cleanup routine. The code is only a Blocks configuration excerpt. It never calls demo.launch, and it is not promised to run after a plain copy.
Task workflow
Default: each event listener has its own queue
The official page says every Gradio app has a built-in queuing system and automatically creates a queue for every event listener. By default each event listener has its own queue, which handles one request at a time. So even if three buttons all generate images, without configuration they are three independent queues, each processing one request at a time. To explain concurrency through a shared resource, you must change this default on purpose.
concurrency_limit: the cap for one listener
concurrency_limit sets the maximum number of concurrent executions for an event listener. The page gives a default of 1 unless configured otherwise in
Blocks.queue(), and you can also set it to None for no limit on concurrent executions. The official single-listener example setsgenerate_btn.clickto 5, meaning that listener can process up to 5 requests at once and extra requests wait until a slot is free. A global default can be set with the default_concurrency_limit parameter ofBlocks.queue().concurrency_id: several listeners on one queue
Assigning the same concurrency_id to different event listeners makes them share a queue. The official scenario is a setup with only 2 GPUs where several functions need GPU access, so a shared queue is created for them. In the official example all three listeners use "gpu_queue" and only the first one writes concurrency_limit=2. The page says that queue handles up to 2 concurrent requests, as defined by concurrency_limit. It does not say in what order requests are served or that the queue binds to particular GPUs, and this article infers neither.
Task and deliverable
When one Gradio page has several event listeners that trigger heavy image generation, you need to say how many queues they use and how many requests can run at once. This article relies only on the official Queuing page. It first covers how the default queue is split by event listener, then how concurrency_limit and concurrency_id change that split, and finally gives an editor-recommended check and cleanup routine. The code is only a Blocks configuration excerpt. It never calls demo.launch, and it is not promised to run after a plain copy.
Prepare the input and environment
- A Python environment where Gradio can be used, and an account or machine on which you are allowed to run the page. This article did not check versions or install anything, so confirm against the official documentation that your Gradio version supports the parameters used below.
- Definitions for image_gen, image_gen_1, image_gen_2 and image_gen_3 made beforehand. The excerpt only references them and has no function bodies. Each function takes a text prompt and returns something that fits a
gr.Image. If these functions call a model or an outside service, you manage any credentials they need. - A way to start the page that you choose yourself. The excerpt does not call
demo.launch, so you must add a launch method in your own runtime before you can observe concurrency.
Mechanism and data flow
Default: each event listener has its own queue: The official page says every Gradio app has a built-in queuing system and automatically creates a queue for every event listener. By default each event listener has its own queue, which handles one request at a time. So even if three buttons all generate images, without configuration they are three independent queues, each processing one request at a time. To explain concurrency through a shared resource, you must change this default on purpose.
concurrency_limit: the cap for one listener: concurrency_limit sets the maximum number of concurrent executions for an event listener. The page gives a default of 1 unless configured otherwise in Blocks.queue(), and you can also set it to None for no limit on concurrent executions. The official single-listener example sets generate_btn.click to 5, meaning that listener can process up to 5 requests at once and extra requests wait until a slot is free. A global default can be set with the default_concurrency_limit parameter of Blocks.queue().
concurrency_id: several listeners on one queue: Assigning the same concurrency_id to different event listeners makes them share a queue. The official scenario is a setup with only 2 GPUs where several functions need GPU access, so a shared queue is created for them. In the official example all three listeners use "gpu_queue" and only the first one writes concurrency_limit=2. The page says that queue handles up to 2 concurrent requests, as defined by concurrency_limit. It does not say in what order requests are served or that the queue binds to particular GPUs, and this article infers neither.
Workflow map
| Stage | What happens |
|---|---|
| Default: each event listener has its own queue | The official page says every Gradio app has a built-in queuing system and automatically creates a queue for every event listener. |
| concurrency_limit: the cap for one listener | concurrency_limit sets the maximum number of concurrent executions for an event listener. |
| concurrency_id: several listeners on one queue | Assigning the same concurrency_id to different event listeners makes them share a queue. |
Complete the task
Confirm the environment and fill in the referenced functions: First confirm your runtime and your permission to use it, then check that image_gen_1, image_gen_2 and image_gen_3 are defined or imported before configuration executes and are accessible to the current code and each accepts the text from prompt. In the excerpt, prompt is a gr.Textbox and image is a gr.Image, and all three functions take prompt as input and write to image. If any function is missing, Python raises an error when it executes the configuration. That is a prerequisite outside the excerpt, not a queue problem.
List every heavy event that uses the same resource: Go through the page and list each event listener that calls an image model and draws on the same kind of resource. This is an editorial recommendation: a listener you leave out keeps its own default queue, so the shared cap does not reach it. In the official example the resource is 2 GPUs. What your actual resource is and how much of it you have is for you to confirm, because the official page does not verify it for you.
Choose a per-listener limit or a shared queue: With a single image event, the official concurrency_limit=5 example is enough: that listener handles up to 5 requests at once and the rest wait. When several events draw on one resource, use concurrency_id instead. The numbers 5 and 2 come from the official examples and are not recommended values for your hardware, so replace them with a capacity you have confirmed.
Write the shared queue configuration: Follow the second official example. The click for generate button 1 sets both concurrency_limit=2 and concurrency_id="gpu_queue", while buttons 2 and 3 set only concurrency_id="gpu_queue" and do not repeat the limit. The three listeners then share gpu_queue with a cap of 2. To reproduce this official example, the editor recommends retaining its one-listener cap declaration. This is a check for this example, not a general shared-queue requirement or a claim that other configurations are invalid.
Run static and semantic checks: This step is an editorial recommendation, not official behavior. For the static check, confirm the three concurrency_id strings match exactly, including case, that reproducing this page's example retains its one-listener limit declaration (not a general requirement), and that every heavy event has been added. For the semantic check, confirm each listener points at the function you actually mean to throttle and that its inputs and outputs map to prompt and image. These checks catch configuration slips. They cannot prove that concurrency or image content is correct.
Launch in your own environment and observe concurrency: After adding demo.launch or your own launch method, trigger several events at the same time in a suitable environment and record how many calls are active and whether later requests wait in the queue. This is an editorial recommendation. The expectation is no more than 2 active calls with the rest waiting, but that is a reader expectation only. No product or API was run in this batch, so there is no actual observation, and you should not infer request order from it.
Finish by outcome, with cleanup on every exit path: This is an editorial recommendation. The official page offers no cancellation API, and this article does not assume one. On success, record the numbers you saw, then stop the page process you started. On failure, keep the error message, stop the process, and return to the earlier steps to check the function definitions and configuration. On empty output, first check whether the function returned something a gr.Image can show, and do not blame the queue directly. If you decide to stop partway, end the process by hand in your own runtime. On every path, remove any credentials or environment variables you set temporarily for the model functions and close the pages you opened.
Worked example
The official example has a textbox prompt, an image component image, and three buttons labeled to generate an image via model 1, model 2 and model 3. The click events call image_gen_1, image_gen_2 and image_gen_3 in turn, with prompt as input and image as output. The first event writes concurrency_limit=2, and all three write concurrency_id="gpu_queue". Per the official description the three listeners share that queue and at most 2 requests are processed at once. If three clicks arrive together, this configuration leads you to expect at most 2 in progress and one waiting. That is a reading of the official text, not an observation, and nothing is said about the order in which the waiting request is served. The excerpt has no demo.launch and does not define the three functions, so it is a configuration sketch, not a complete program.
import gradio as gr
with gr.Blocks() as demo:
prompt = gr.Textbox()
image = gr.Image()
generate_btn = gr.Button("Generate Image")
generate_btn.click(image_gen, prompt, image, concurrency_limit=5)import gradio as gr
with gr.Blocks() as demo:
prompt = gr.Textbox()
image = gr.Image()
generate_btn_1 = gr.Button("Generate Image via model 1")
generate_btn_2 = gr.Button("Generate Image via model 2")
generate_btn_3 = gr.Button("Generate Image via model 3")
generate_btn_1.click(image_gen_1, prompt, image, concurrency_limit=2, concurrency_id="gpu_queue")
generate_btn_2.click(image_gen_2, prompt, image, concurrency_id="gpu_queue")
generate_btn_3.click(image_gen_3, prompt, image, concurrency_id="gpu_queue")Check the result and failure paths
- Success (editorial recommendation): every heavy event uses the same concurrency_id, your observation shows no more active calls than the shared cap with extra requests waiting, and each successful request puts its result into image.
- Failure (editorial recommendation): a function is undefined or raises an error, the concurrency_id spellings differ, a heavy event was left out of the shared queue, or you observe more active calls than the cap you set. On any of these, stop, fix it and check again.
- Empty output: when the image component shows nothing, look at the corresponding function's return value and the input prompt before deciding whether it is a concurrency issue. The official page says nothing about empty output, so queue configuration alone cannot explain it.
- User stopping: when you decide to abort, end the process you started by hand. Do not assume a cancellation API exists, and do not treat ending the process as official guaranteed behavior. It is only your own manual step, followed by the credential and page cleanup from step 7.
Scope and limits
- The official page explains only the configuration meaning of queues and concurrency counts. It does not state request ordering, automatic binding of the queue to GPUs, a guarantee of exclusive resource access, or distributed rate limiting across machines. This article makes no claim about any of these.
- concurrency_limit=None means no limit on concurrent executions, and the page says it suits cases where the function calls an external API that handles its own request rate limiting. None does not mean your service rate is compliant, and it does not confirm the outside service's limits. You must confirm those yourself.
No product or API was run in this batch, Gradio was not installed, and demo.launch was not called. Every statement about Gradio queue behavior comes from the official Queuing page. The static and semantic checks, success and failure criteria, stopping conditions and credential cleanup are editorial recommendations, not official behavior. The official examples are also not actual observations from this batch.
Article sources
Original editorial content; official descriptions and checking suggestions are distinguished.