Concurrency API ​
The concurrency module provides tools for parallel execution and task management.
Functions ​
These functions are available under httpx.concurrency.* and also as top-level helpers:
httpx.allhttpx.anyhttpx.racehttpx.allSettled
Additional top-level aliases:
httpx.first(alias forhttpx.any)httpx.fastest(alias forhttpx.race)httpx.settled(alias forhttpx.allSettled)
all ​
Executes multiple requests in parallel and waits for all to complete.
pub fn all(allocator: Allocator, client: *Client, specs: []const RequestSpec) ![]RequestResultallSettled ​
Executes multiple requests in parallel and returns a result for each request.
- Successful requests are returned as
RequestResult.success. - Failed requests are returned as
RequestResult.err.
pub fn allSettled(allocator: Allocator, client: *Client, specs: []const RequestSpec) ![]RequestResultany ​
Executes multiple requests and returns the first successful (2xx) response.
pub fn any(allocator: Allocator, client: *Client, specs: []const RequestSpec) !?Responserace ​
Executes multiple requests and returns the result of the first one to complete (success or error).
pub fn race(allocator: Allocator, client: *Client, specs: []const RequestSpec) !RequestResultTop-Level Alias Signatures ​
pub fn first(allocator: Allocator, client: *Client, specs: []const RequestSpec) !?Response
pub fn fastest(allocator: Allocator, client: *Client, specs: []const RequestSpec) !RequestResult
pub fn settled(allocator: Allocator, client: *Client, specs: []const RequestSpec) ![]RequestResultBatchBuilder ​
Use httpx.BatchBuilder to compose request batches fluently.
var builder = httpx.BatchBuilder.init(allocator);
defer builder.deinit();
_ = try builder.get("https://api.example.com/users");
_ = try builder.post("https://api.example.com/users", "{\"name\":\"demo\"}");| Method | Description |
|---|---|
get(url) | Add GET request |
post(url, body) | Add POST request |
put(url, body) | Add PUT request |
delete(url) | Add DELETE request |
add(spec) | Add explicit RequestSpec |
count() | Number of queued requests |
clear() | Remove queued requests |
Executor ​
A thread-pool based task executor.
const httpx = @import("httpx");
var executor = httpx.Executor.init(allocator);
defer executor.deinit();Configuration ​
pub const ExecutorConfig = struct {
num_threads: u32 = 0, // 0 = auto-detect
task_queue_size: usize = 1024,
idle_timeout_ms: u64 = 60_000,
};Methods ​
execute ​
Submits a function for execution.
pub fn execute(self: *Self, func: TaskFn, context: ?*anyopaque) !voidsubmit ​
Submits a Task struct.
pub fn submit(self: *Self, task: Task) !voidrunAll ​
Runs all pending tasks synchronously (useful for testing).
pub fn runAll(self: *Self) voidstart / stop ​
Start and stop worker threads explicitly.
pub fn start(self: *Self) !void
pub fn stop(self: *Self) voidpendingCount ​
Returns a snapshot count of queued tasks.
pub fn pendingCount(self: *const Self) usizeTypes ​
Task ​
represents a unit of work.
pub const Task = struct {
func: TaskFn,
context: ?*anyopaque = null,
priority: u8 = 0,
};TaskFn ​
pub const TaskFn = *const fn (?*anyopaque) void;RequestSpec ​
Specification for a request in a batch operation.
pub const RequestSpec = struct {
method: Method = .GET,
url: []const u8,
body: ?[]const u8 = null,
headers: ?[]const [2][]const u8 = null,
};RequestResult ​
Result wrapper for parallel requests.
pub const RequestResult = union(enum) {
success: Response,
err: anyerror,
// Helper methods
pub fn isSuccess(self: RequestResult) bool
pub fn getResponse(self: *RequestResult) ?*Response
pub fn deinit(self: *RequestResult) void
};