I'm using Tokio 1.1 to do async things. I have an async
main
with #[tokio::main]
so I'm already operating with a runtime.
main
invokes a non-async method where I'd like to be await
on a future (specifically, I'm collecting from a datafusion dataframe). This non-async method has a signature prescribed by a trait which returns a struct, not a Future<Struct>
. As far as I'm aware, I can't mark it async.
If I try and call df.collect().await;
, I get the
only allowed inside
async
functions and blocks
error from the compiler, pointing out that the method that I'm calling await
within is not async
.
If I try and block_on
the future from a new runtime like this:
tokio::runtime::Builder::new_current_thread()
.build()
.unwrap()
.block_on(df.collect());
I get a runtime panic:
Cannot start a runtime from within a runtime. This happens because a function (like
block_on
) attempted to block the current thread while the thread is being used to drive asynchronous tasks.
If I try futures::executor::block_on(df.collect()).unwrap();
, I get a new runtime panic:
'not currently running on a Tokio 0.2.x runtime.'
which is weird because I'm using Tokio v1.1.
This feels harder than it should. I'm within an async context, and it feels like the compiler should know that and allow me to call .await
from within the method - the only code path invokes this method from within an async
block. Is there a simple way to do this that I'm missing?
awaiting
inside a sync functions just doesn't fundamentally work. You can spawn a blocking task withtokio::spawn_blocking
. – Underwingspawn_blocking
, I'll take a look and update. Thanks! – BadinageJoinHandle
which still needs to beawait
-ed - unless I'm missing something? – Badinage