advance-practice/channels #901
Replies: 9 comments 13 replies
-
您好,我想问一下,tokio是不是无法完成golang中使用channel控制并发实现事件驱动并处理消息的功能呢? 我希望通过一个loop不断接收client请求,为每一个client连接的tcpstream分配spawn用于处理,使用一个一对多的channel,将每个client连接的请求通过channel发送 使用一个独立的spawn监听channel,一旦有发送事件到来就接收消息然后处理并将结果回写给client 所以我尝试对tcpstream进行读写分离,通过arc将writehalf在spawn之间进行共享,这样只需要在loop外单独使用一个spawn监听接收通道即可,接收通道的所有权只需要转移给这一个spawn,没有问题 具体的做法是将client请求和该client对应的tcpstream中的writehalf封装成一个message通过channel发送 目前我也想不到其他的方法能够实现该功能了 我想请教一下是tokio确实无法完成这样的功能,还是我的使用方法不对? |
Beta Was this translation helpful? Give feedback.
-
这代码有点乱呀,东一块,西一块的。 |
Beta Was this translation helpful? Give feedback.
-
有个疑问,在上一节共享状态中的有一小节 “初始化 HashMap” 的代码如下: #[tokio::main]
} 我的疑问是 为什么这里的tokio::spawn() 不用.await 而本节的tokio::spawn代码需要用.await 呢? |
Beta Was this translation helpful? Give feedback.
-
resp_rx 看着是个Receiver类型,为啥可以调用.await呢 |
Beta Was this translation helpful? Give feedback.
-
hello 您好,我有个问题想请教一下。 |
Beta Was this translation helpful? Give feedback.
-
把t2.await.unwrap();做了注释依然会去执行t2任务,请问是let tx2 = tx.clone();的问题嘛,把tx2发送器注释了就不会执行了 |
Beta Was this translation helpful? Give feedback.
-
上面提到:“有一点值得注意,往 oneshot 中发送消息时,并没有使用 .await,原因是该发送操作要么直接成功、要么失败,并不需要等待。” |
Beta Was this translation helpful? Give feedback.
-
while let Some(cmd) = rx.recv().await {
match cmd {
Command::Get { key, resp } => {
let res = client.get(&key).await;
// 忽略错误
let _ = resp.send(res);
}
Command::Set { key, val, resp } => {
let res = client.set(&key, val).await;
// 忽略错误
let _ = resp.send(res);
}
}
} 这个地方发往redis的请求还是串行的, 怎么做到并行? 在第一个请求得到结果之前就发出第二个请求 |
Beta Was this translation helpful? Give feedback.
-
有一点值得注意,往 oneshot 中发送消息时,并没有使用 .await,原因是该发送操作要么直接成功、要么失败,并不需要等待。 当 oneshot 的接受端被 drop 后,继续发送消息会直接返回 Err 错误,它表示接收者已经不感兴趣了。对于我们的场景,接收者不感兴趣是非常合理的操作,并不是一种错误,因此可以直接忽略。 有点不是很理解,上面不是写到Future不await的时候是不会执行的吗 怎么resp.send(res);会执行呢? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
advance-practice/channels
https://course.rs/advance-practice/channels.html
Beta Was this translation helpful? Give feedback.
All reactions