Ubuntu 22.04 安装 Apache Asyncband 0.7.3 指南

Ubuntu 22.04 实测安装 asyncband 0.7.3:rustup 安装与版本核验、cargo 工程初始化、依赖引入、debug/release 构建,barrier/semaphore/mutex 三场景运行验证。

Apache AsyncbandAsyncband2026/10/14

Ubuntu 22.04 安装 Apache Asyncband 0.7.3 指南

目标:在 Ubuntu 22.04 上完成 Apache Asyncband(Apache 孵化器出品的 Rust 异步同步原语库)0.7.3 的安装与最小可用验证——覆盖编译依赖核验、rustup 一键安装与工具链版本核验、cargo 工程初始化、asyncband 依赖引入、debug/release 构建以及 barrier/semaphore/mutex 三场景运行验证。全部命令已在 Ubuntu 22.04.5 LTS(rustc/cargo 1.99.0)实测通过,回显可复现。

一、环境信息

  • 操作系统:Ubuntu 22.04.5 LTS(jammy),x86_64
  • 工具链:rustup 1.29.1(工具链管理器)、rustc 1.99.0、cargo 1.99.0(stable-x86_64-unknown-linux-gnu)
  • 组件版本:asyncband 0.7.3(crates.io 最新稳定版,Apache-2.0)、tokio 1.53.2(异步运行时,仅用于驱动 demo)
  • 实验目录:/root/asyncband-demo(cargo 二进制工程)
  • 执行用户:root

Asyncband 是 Rust 库而非独立服务,所谓"安装"即三步:工具链就绪、cargo 工程引入依赖、构建并运行验证。

二、前置条件:编译依赖核验

引导:Rust 编译产物的链接依赖 C 编译器与链接器,先核验 gcc/cc/ld/make/curl:

type gcc cc make curl && ls /usr/bin/ | grep -E "^(gcc|cc|ld|make)$"

回显(实测):

gcc is /usr/bin/gcc
cc is /usr/bin/cc
make is /usr/bin/make
curl is /usr/bin/curl
gcc
cc
ld
make

验证结果:编译工具齐备。缺失时 apt-get update && apt-get install -y build-essential curl 一次性补齐(build-essential 元包含 gcc/cc/ld/make)。

三、安装 rustup 与 Rust 工具链

引导:官方推荐 rustup 安装 Rust(可管理多工具链、按需更新)。本机 rustup 已预装且工具链完好,以下为完整安装命令与实测核验回显。全新环境安装命令(0 到 1):

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs -o /tmp/rustup-init.sh
bash /tmp/rustup-init.sh -y --profile default --default-toolchain stable
source "$HOME/.cargo/env"

脚本下载核验(实测):

rustup-init.sh downloaded
#!/bin/sh
# shellcheck shell=dash
29915 bytes

rustup 已安装环境的版本核验(实测):

export PATH=$PATH:/root/.cargo/bin
rustup --version && rustc --version && cargo --version

回显(实测):

rustup 1.29.1 (d95a37b6a 2026-08-13)
info: This is the version for the rustup toolchain manager, not the rustc compiler.
info: the currently active `rustc` version is `rustc 1.99.0 (b940084d7 2026-09-28)`
cargo 1.99.0 (5f94df478 2026-08-27)
rustc 1.99.0 (b940084d7 2026-09-28)

工具链状态核验:

rustup toolchain list && rustup show | head -12

回显(实测):

stable-x86_64-unknown-linux-gnu (active, default)
Default host: x86_64-unknown-linux-gnu
rustup home:  /root/.rustup

installed toolchains
--------------------
stable-x86_64-unknown-linux-gnu (active, default)

active toolchain
----------------
name: stable-x86_64-unknown-linux-gnu
active because: it's the default toolchain
installed targets:
  x86_64-unknown-linux-gnu

验证结果:rustup 1.29.1、rustc 1.99.0、cargo 1.99.0 齐备,default toolchain 为 stable-x86_64-unknown-linux-gnu,目标 x86_64-unknown-linux-gnu 已安装。

四、创建 cargo 工程并引入 asyncband 依赖

引导:cargo init 初始化二进制工程,cargo add 引入依赖并自动写入 Cargo.toml。asyncband 默认不启用任何 API,须按需开启 feature;本 demo 使用 barrier、semaphore、mutex 三个 feature,运行时选择 tokio(macros 与 rt-multi-thread 两个 feature 足够):

mkdir -p /root/asyncband-demo && cd /root/asyncband-demo
cargo init --name asyncband-demo
cargo add asyncband --features barrier,semaphore,mutex
cargo add tokio --features macros,rt-multi-thread
cat Cargo.toml

回显(实测,cargo init 与两次 cargo add 的关键行):

    Creating binary (application) package
    Updating crates.io index
      Adding asyncband v0.7.3 to dependencies
             Features:
             + barrier
             + mutex
             + semaphore
             - blocking
             - broadcast
             - completion
             - condvar
             - event
             - latch
             - lazy-cell
             - mpmc
             - mpsc
             - once
             - once-cell
             - once-map
             - oneshot
             - phaser
             - pool
             - rwlock
             - shutdown
             - singleflight
             - spmc
             - waitgroup
             - watch
    Updating crates.io index
     Locking 1 package to highest Rust 1.99.0 compatible version
      Adding tokio v1.53.2
      Adding tokio-macros v2.7.2

Cargo.toml 生成结果:

[package]
name = "asyncband-demo"
version = "0.1.0"
edition = "2024"

[dependencies]
asyncband = { version = "0.7.3", features = ["barrier", "semaphore", "mutex"] }
tokio = { version = "1.53.2", features = ["macros", "rt-multi-thread"] }

💡 技术拓展:异步运行时生态。Asyncband 是 runtime-agnostic 库——不 spawn 任务、不持有工作线程、不安装定时器,异步 API 只建立在标准 future 与 waker 之上,由调用方选择运行时去 poll 它。Tokio 是功能最全的多线程运行时(rt-multi-thread + macros 即可驱动本 demo);async-std 提供与标准库一致的 API 面;smol 主打极简(公开 API 仅约 40 个)。三者任选其一都可驱动 asyncband,切换时只需调整依赖与 #[tokio::main] 入口宏,同步原语代码零改动。

验证结果:依赖锁定 asyncband 0.7.3、tokio 1.53.2(cargo add 自动选择当前 stable 兼容的最高版本),feature 列表确认 barrier/mutex/semaphore 三个已启用。

五、构建验证:debug 与 release

引导:cargo 默认按 debug profile 构建(编译快、含调试信息),发布部署用 release(开启优化)。

debug 构建:

cd /root/asyncband-demo && cargo build

回显(实测):

   Compiling proc-macro2 v1.0.107
   Compiling unicode-ident v1.0.27
   Compiling quote v1.0.47
   Compiling syn v3.0.9
   Compiling pin-project-lite v0.2.17
   Compiling tokio-macros v2.7.2
   Compiling tokio v1.53.2
   Compiling asyncband-demo v0.1.0 (/root/asyncband-demo)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 8.61s

验证结果:构建成功、零警告。依赖树中 asyncband 为叶子节点(0.7.3 零传递依赖),tokio 及过程宏全部编译通过。

release 构建:

cargo build --release

回显(实测):

   Compiling tokio-macros v2.7.2
   Compiling tokio v1.53.2
   Compiling asyncband-demo v0.1.0 (/root/asyncband-demo)
    Finished `release` profile [optimized] target(s) in 8.49s

real    0m8.519s

验证结果:release 产物位于 target/release/asyncband-demo,构建耗时约 8.5 秒。

六、运行验证:barrier/semaphore/mutex 三场景

引导:demo 程序依次演示三个同步原语——Semaphore 限流(8 个任务竞争 2 个许可)、Barrier 阶段同步(4 个参与者齐至后放行、每代恰一个 leader)、Mutex 保护共享计数(8 个任务并发自增)。源码写入 src/main.rs 后运行:

cd /root/asyncband-demo && cargo run

回显(实测):

    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.63s
     Running `target/debug/asyncband-demo`
task 0: acquired semaphore permit
task 2: acquired semaphore permit
task 4: acquired semaphore permit
task 1: acquired semaphore permit
task 5: acquired semaphore permit
task 6: acquired semaphore permit
task 3: acquired semaphore permit
task 7: acquired semaphore permit
semaphore stage done, available permits = 2
task 3: passed barrier (leader=true)
task 0: passed barrier (leader=false)
task 1: passed barrier (leader=false)
task 2: passed barrier (leader=false)
mutex stage done, final counter = 8

验证结果:三场景语义全部正确——8 个任务依次获取许可、全部释放后可用许可恢复为 2;4 个参与者通过屏障且每代恰好 1 个 leader;Mutex 保护下并发自增 8 次终值精确为 8。Asyncband 0.7.3 安装完成、库可正常使用。

七、常见问题与排查

  1. crates.io 拉取依赖慢或超时:先 export http_proxy=http://192.168.1.62:7897 https_proxy=http://192.168.1.62:7897 再执行 cargo 命令;代理只需在依赖下载阶段开启。
  2. rustc 报 missing manifest in toolchain:工具链部分损坏,用 rustup toolchain install stable --force 强制重装。
  3. 链接报 cc 缺失:apt-get update && apt-get install -y build-essential 补齐编译工具链。
  4. source "$HOME/.cargo/env" 每次登录需重新执行,或把该行追加到 ~/.bashrc 实现持久化。

八、参考链接

  1. Apache Asyncband 官方站点:https://asyncband.apache.org
  2. crates.io 上的 asyncband:https://crates.io/crates/asyncband
  3. docs.rs 文档:https://docs.rs/asyncband
  4. GitHub 源码仓库:https://github.com/apache/asyncband
  5. rustup 官方安装文档:https://rustup.rs
  6. Ubuntu 软件包(build-essential):https://packages.ubuntu.com/jammy/build-essential