< 返回版块

fengqi2019 发表于 2021-10-01 00:24

结论:直接调用b.poll_read即可

描述的有点乱,请见谅,如果需要额外说明,请留言 问题如代码:

struct A{
   b: B
}
impl tokio::io::AsyncRead for B {...略...}

impl tokio::io::AsyncRead for A {
   # 如何调用B的AsyncRead.使得当B有数据时,可以唤醒A的AsyncRead?
}

目前我的做法是通过额外的异步任务+通道实现,伪代码如下,请问有更合适的办法吗?能否在唤醒B的时候,直接唤醒A?

async fn middle() {
	let waker = Option<Waker>;
	select! {
		read_buf(b) => {
			send_channel(data);
			waker.wake()
		}
		recv_channel(waker_tmp) => {
			let waker = Some(waker_tmp);
		}
	}
}

评论区

写评论
作者 fengqi2019 2021-10-02 10:33

抱歉,直接调用b的poll_read即可,之前是因为我自己实现,没有唤醒,才导致程序的不正常!

--
👇
songzhi: 还有一个ready!宏

--
👇
songzhi: 不是有asyncreadext吗?

--
👇
fengqi2019: tokio::io::AsyncRead的fn poll_read不是异步的,没法直接await

--
👇
songzhi: b.read(&mut buf).await

作者 fengqi2019 2021-10-01 23:05

调用ready!,若结果是Pending,则后续无法再次唤醒该异步任务

--
👇
songzhi: 还有一个ready!宏

--
👇
songzhi: 不是有asyncreadext吗?

--
👇
fengqi2019: tokio::io::AsyncRead的fn poll_read不是异步的,没法直接await

--
👇
songzhi: b.read(&mut buf).await

songzhi 2021-10-01 13:15

还有一个ready!宏

--
👇
songzhi: 不是有asyncreadext吗?

--
👇
fengqi2019: tokio::io::AsyncRead的fn poll_read不是异步的,没法直接await

--
👇
songzhi: b.read(&mut buf).await

songzhi 2021-10-01 13:13

不是有asyncreadext吗?

--
👇
fengqi2019: tokio::io::AsyncRead的fn poll_read不是异步的,没法直接await

--
👇
songzhi: b.read(&mut buf).await

作者 fengqi2019 2021-10-01 12:46

tokio::io::AsyncRead的fn poll_read不是异步的,没法直接await

--
👇
songzhi: b.read(&mut buf).await

songzhi 2021-10-01 09:35

b.read(&mut buf).await

1 共 6 条评论, 1 页