slot_decay_per_second. With a concurrency limit, slots are released when the concurrency manager exits.
Manage Global concurrency limits and rate limits
Create, read, edit, and delete concurrency limits through the Prefect UI. When creating a concurrency limit, you can specify the following parameters:- Name: The name of the concurrency limit. This name is also how you’ll reference the concurrency limit in your code. Special characters,
such as
/,%,&,>,<, are not allowed. - Concurrency Limit: The maximum number of slots that can be occupied on this concurrency limit.
- Slot Decay Per Second: Controls the rate at which slots are released when the concurrency limit is used as a rate limit. You must
configure this value when using the
rate_limitfunction. - Active: Whether or not the concurrency limit is in an active state.
Active vs. inactive limits
Global concurrency limits can be in anactive or inactive state:
- Active: In this state, slots can be occupied, and code execution is blocked when slots are unable to be acquired.
- Inactive: In this state, slots are not occupied, and code execution is not blocked. Concurrency enforcement occurs only when you activate the limit.
Slot decay
You can configure global concurrency limits with slot decay. Use this when you want a rate limit. It will govern the pace at which slots are released or become available for reuse after being occupied. These slots represent the concurrency capacity within a specific concurrency limit. This is the rate at which these slots “decay” or refresh. To configure slot decay, set theslot_decay_per_second parameter when defining or adjusting a concurrency limit.
For practical use, consider the following:
- Higher values: Setting
slot_decay_per_secondto a higher value, such as 5.0, results in slots becoming available relatively quickly. In this scenario, a slot that was occupied by a task will free up after just0.2(1.0 / 5.0) seconds. - Lower values: Conversely, setting
slot_decay_per_secondto a lower value, like 0.1, causes slots to become available more slowly. In this scenario it takes10(1.0 / 0.1) seconds for a slot to become available again after occupancy.
Through the UI
You can create, read, edit, and delete concurrency limits through the Prefect UI. When creating a concurrency limit, you can specify the following parameters:- Name: The name of the concurrency limit. This name is also how to reference the concurrency limit in your code. Special characters,
such as
/,%,&,>,<, are not allowed. - Concurrency Limit: The maximum number of slots that can be occupied on this concurrency limit.
- Slot Decay Per Second: Controls the rate at which slots are released when the concurrency limit is used as a rate limit. You must configure this
value when using the
rate_limitfunction. - Active: Whether or not the concurrency limit is in an active state.
Through the CLI
You can create, read, edit, and delete global concurrency limits through the Prefect CLI. To create a new concurrency limit, use theprefect gcl create command. You must specify a --limit argument, and can optionally specify a
--slot-decay-per-second and --disable argument.
prefect gcl inspect command:
prefect gcl update command. You can update the --limit, --slot-decay-per-second, --enable,
and --disable arguments:
prefect gcl delete command:
prefect gcl --help.
Using the concurrency context manager
The concurrencycontext manager allows control over the maximum number of concurrent operations. Select either the synchronous (sync)
or asynchronous (async) version, depending on your use case. Here’s how to use it:
Sync
- The code imports the necessary modules and the concurrency context manager. Use the
prefect.concurrency.syncmodule for sync usage and theprefect.concurrency.asynciomodule for async usage. - It defines a
process_datatask, takingxandyas input arguments. Inside this task, the concurrency context manager controls concurrency, using thedatabaseconcurrency limit and occupying one slot. If another task attempts to run with the same limit and no slots are available, that task is blocked until a slot becomes available. - A flow named
my_flowis defined. Within this flow, it iterates through a list of tuples, each containing pairs of x and y values. For each pair, theprocess_datatask is submitted with the corresponding x and y values for processing.
Using rate_limit
The Rate Limit feature provides control over the frequency of requests or operations, ensuring responsible usage and system stability.
Depending on your requirements, you can use rate_limit to govern both synchronous (sync) and asynchronous (async) operations.
Here’s how to make the most of it:
Sync
- The code imports the necessary modules and the
rate_limitfunction. Use theprefect.concurrency.syncmodule for sync usage and theprefect.concurrency.asynciomodule for async usage. - It defines a
make_http_requesttask. Inside this task, therate_limitfunction ensures that the requests are made at a controlled pace. - A flow named
my_flowis defined. Within this flow themake_http_requesttask is submitted 10 times.
Use concurrency and rate_limit outside of a flow
Useconcurrency and rate_limit outside of a flow to control concurrency and rate limits for any operation.
Use cases
Throttling task submission
Throttling task submission helps avoid overloading resources, complying with external rate limits, or ensuring a steady, controlled flow of work. In this scenario therate_limit function throttles the submission of tasks. The rate limit acts as a bottleneck, ensuring
that tasks are submitted at a controlled rate, governed by the slot_decay_per_second setting on the associated concurrency limit.
Manage database connections
Manage the maximum number of concurrent database connections to avoid exhausting database resources. This scenario uses a concurrency limit nameddatabase. It has a maximum concurrency limit that matches the maximum number
of database connections. The concurrency context manager controls the number of database connections
allowed at any one time.
notest
Parallel data processing
Limit the maximum number of parallel processing tasks. This scenario limits the number ofprocess_data tasks to five at any one time. The concurrency
context manager requests five slots on the data-processing concurrency limit. This blocks until five slots are free and then
submits five more tasks, ensuring that the maximum number of parallel processing tasks is never exceeded.