How to use tokio::fs::File in sync way?
#7690
-
|
My application starts But in such async runtime, sometimes I have special needs that I need to call tokio APIs in a I actually have several possible solutions (but still not sure which one is the best/recommended): TryIntoStd/FromStd
With these 2 APIs, I could first convert One more question is: I found there are some buffers and locks inside Lines 90 to 94 in 5dacc2e Lines 489 to 521 in 5dacc2e Will tokio release the Does Tokio Runtime provide some helper functions?Does tokio runtime provide some helper functions, that can run a Or I can simply use below code? fn my_sync_function(filename: &Path) -> tokio::fs::File {
tokio::runtime::Handle::current().block_on(async {
tokio::fs::File::open(filename).await
})
}ReferenceHere is how I send a message to tokio::runtime::Handle::current().spawn_blocking(move || {
master_tx.blocking_send(message).unwrap();
})
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
|
You can do something along these lines: let std_file = Handle::current().block_on(tokio_file.into_std());
std_file.write(...);
let tokio_file = File::from_std(std_file); |
Beta Was this translation helpful? Give feedback.
You can do something along these lines: