1 个不稳定版本
0.1.0 | 2024年2月28日 |
---|
#909 in 异步
每月 21 次下载
用于 may_rpc
10KB
128 行
co_managed
此库可以创建受管理的子协程。
当它们的父进程退出时,受管理的子协程将被取消。这类似于作用域内协程的创建,不同之处在于我们在散列表中管理子协程,因此当子协程退出时,条目会动态删除,父进程不需要等待子进程退出。
用法
首先,将以下内容添加到您的 Cargo.toml
[dependencies]
co_managed = "0.1"
然后只需简单地实现您的http服务
use may::{go, coroutine};
use co_managed::Manager;
use std::time::Duration;
fn main() {
let j = go!(|| {
println!("parent started");
let manager = Manager::new();
struct Dummy(usize);
impl Drop for Dummy {
fn drop(&mut self) {
println!("co dropped, id={}", self.0);
}
}
for i in 0..10 {
manager.add(move |_| {
let d = Dummy(i);
println!("sub started, id = {}", d.0);
loop {
coroutine::sleep(Duration::from_millis(10));
}
});
}
coroutine::park();
});
coroutine::sleep(Duration::from_millis(100));
unsafe { j.coroutine().cancel() };
j.join().ok();
println!("parent exit");
coroutine::sleep(Duration::from_millis(1000));
}
许可协议
您可以选择以下任一许可协议来使用此项目
- Apache License, Version 2.0, (LICENSE-APACHE 或 https://apache.ac.cn/licenses/LICENSE-2.0)
- MIT License (LICENSE-MIT 或 http://opensource.org/licenses/MIT)
依赖关系
~3–29MB
~421K SLoC