-
Version Platform Description [code sample that causes the bug]
I expected to see this happen: [explanation] If I remove the futures::executor::block_on() block and I just keep the loop, it works well. But as soon as I add a block_on block, if lot of operation occur, it blocks somewhere. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 2 replies
-
I see the issue. You shouldn’t be using futures::executor alongside with Tokio as neither executor (Tokio or futures) are aware of one another, which explains the blocking behavior you’re seeing. I’ll also note that |
Beta Was this translation helpful? Give feedback.
-
Thank you for your quick reply. I understand your point. But what I can do to block on a async function call in a such case ? Of course, in the example, it doesn't make sense to add a block_on() block there. But in my real case, I have a callback defined like this : And into the callback, I want to call an async function. The callback is called from a thread running within a runtime. Even if I use tokio::runtime::block_on, it doesn't work because you can't start a runtime from within a runtime. Do I have any option there ? Of course, I could change my callback to look like Fn(u32, &[u8]) -> BoxFuture<(Vec, Option)> and remove the block_on() block, but it is not as simple as that since the callback is from a future 1 that is launch on the tokio runtime with a call to compat(). So I though that I could block on the async call as a quick fix. Any idea how I could block in a such case ? Thanks |
Beta Was this translation helpful? Give feedback.
-
I'm not entirely sure what you mean by "block on an async call", as I can see two possible interpretations for that. However, I think you're looking for the second usecase:
use std::time::Duration;
use tokio::{task, time};
fn callback(i: u32, byte: &[u8]) -> (Vec<i32>, Option) {
// skipping details...
}
#[tokio::main]
async fn main() {
task::spawn_blocking(|| {
for i in 0..100 {
let bytes = vec![];
callback(i, bytes)
}
});
} Did I get that right? |
Beta Was this translation helpful? Give feedback.
-
Not exactly. I want to call an async function in my callback. And that function is not async so I can't do a simple .await on my call. Here is a simple example of what I have. I spawn a future version 1 on the tokio runtime with the compat() call. And into that future, in the poll function, I call my callback that is not async. But in my callback, I want to call an async function. Is it possible ?
And even if would change my callback function to be async, let say :
I can't call .await in my poll() function. I hope it is clear. Of course, I would like to remove all those future 1, but I have to understand how to do it and learn more how to migrate from 1 to 3. Thanks |
Beta Was this translation helpful? Give feedback.
-
Apparently I cannot mark the comment above as answer, so I'll mark this one instead. |
Beta Was this translation helpful? Give feedback.
Apparently I cannot mark the comment above as answer, so I'll mark this one instead.