Skip to content

Instantly share code, notes, and snippets.

@andrewrk
Created June 21, 2024 03:52
Show Gist options
  • Save andrewrk/985de049693100d30e15ab3940bf601f to your computer and use it in GitHub Desktop.
Save andrewrk/985de049693100d30e15ab3940bf601f to your computer and use it in GitHub Desktop.
demo of jobserver
const std = @import("std");
pub fn main() !void {
const arena = std.heap.page_allocator;
var thread_pool: std.Thread.Pool = undefined;
try thread_pool.init(.{
.allocator = arena,
.job_server = .{ .connect = try std.net.Address.initUnix("tmp_socket") },
.n_jobs = 4,
});
defer thread_pool.deinit();
const root_node = std.Progress.start(.{
.root_name = "inside child one",
});
defer root_node.end();
const child_node = root_node.start("grand child process node", 0);
defer child_node.end();
var wg: std.Thread.WaitGroup = .{};
defer wg.wait();
thread_pool.spawnWg(&wg, fakeWork, .{ root_node, "child worker 1" });
thread_pool.spawnWg(&wg, fakeWork, .{ root_node, "child worker 2" });
thread_pool.spawnWg(&wg, fakeWork, .{ root_node, "child worker 3" });
thread_pool.spawnWg(&wg, fakeWork, .{ root_node, "child worker 4" });
var child = std.process.Child.init(&.{"./child2"}, arena);
child.progress_node = child_node;
child.thread_pool = &thread_pool;
try child.spawn();
const term = try child.wait();
_ = term;
}
fn fakeWork(parent_node: std.Progress.Node, name: []const u8) void {
const node = parent_node.start(name, 8);
defer node.end();
for (0..8) |_| {
std.time.sleep(600 * std.time.ns_per_ms);
node.completeOne();
}
}
const std = @import("std");
pub fn main() !void {
const arena = std.heap.page_allocator;
var thread_pool: std.Thread.Pool = undefined;
try thread_pool.init(.{
.allocator = arena,
.job_server = .{ .connect = try std.net.Address.initUnix("tmp_socket") },
.n_jobs = 4,
});
defer thread_pool.deinit();
const root_node = std.Progress.start(.{
.root_name = "inside child one",
});
defer root_node.end();
var wg: std.Thread.WaitGroup = .{};
defer wg.wait();
thread_pool.spawnWg(&wg, fakeWork, .{ root_node, "grand child worker 1" });
thread_pool.spawnWg(&wg, fakeWork, .{ root_node, "grand child worker 2" });
thread_pool.spawnWg(&wg, fakeWork, .{ root_node, "grand child worker 3" });
}
fn fakeWork(parent_node: std.Progress.Node, name: []const u8) void {
const node = parent_node.start(name, 10);
defer node.end();
for (0..10) |_| {
std.time.sleep(400 * std.time.ns_per_ms);
node.completeOne();
}
}
https://asciinema.org/a/lMKZ0YZceqBLsww419YxFxjxT
const std = @import("std");
pub fn main() !void {
const arena = std.heap.page_allocator;
var thread_pool: std.Thread.Pool = undefined;
try thread_pool.init(.{
.allocator = arena,
.job_server = .{ .host = try std.net.Address.initUnix("tmp_socket") },
.n_jobs = 4,
});
defer thread_pool.deinit();
const root_node = std.Progress.start(.{
.root_name = "root node",
});
defer root_node.end();
const child_node = root_node.start("child process node", 0);
defer child_node.end();
var wg: std.Thread.WaitGroup = .{};
defer wg.wait();
thread_pool.spawnWg(&wg, fakeWork, .{ root_node, "in house worker 1" });
thread_pool.spawnWg(&wg, fakeWork, .{ root_node, "in house worker 2" });
var child = std.process.Child.init(&.{"./child"}, arena);
child.progress_node = child_node;
child.thread_pool = &thread_pool;
try child.spawn();
const term = try child.wait();
_ = term;
}
fn fakeWork(parent_node: std.Progress.Node, name: []const u8) void {
const node = parent_node.start(name, 10);
defer node.end();
for (0..10) |_| {
std.time.sleep(500 * std.time.ns_per_ms);
node.completeOne();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment