-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is it possible to use LocalSet from within a spawned task? #2095
Comments
You cannot use regular #[tokio::main]
async fn main() {
tokio::spawn_blocking(move {
let rt = tokio::runtime::Handle::current();
rt.block_on(async {
let local = tokio::task::LocalSet::new();
local.run_until(async {}).await;
});
}).await.unwrap()
} |
Thank you for the explanation, @carllerche! Unfortunately, latest version of How can I do the same with the current API? Thanks! |
Oh sorry... I thought we were going to add it. Somebody still needs to write that PR :) You can do this instead: let rt = runtime::Handle::current();
rt.enter(|| {
futures::executor::block_on(...)
}) |
Thank you! Does exactly what I need 🙂 Hopefully there'll be a corresponding PR for |
Version
tokio-macros v0.2.3
tokio v0.2.9
Platform
MacOS 10.15.2:
Darwin 19.2.0
Darwin Kernel Version 19.2.0: Sat Nov 9 03:47:04 PST 2019; root:xnu-6153.61.1~20/RELEASE_X86_64 x86_64
Description
I can't find a way to use
LocalSet
inside a task spawned bytokio::spawn
inside a#[tokio::main]
main fn.I tried this code:
In this case, the compiler complains that:
From my understanding, it tries to capture a
Scheduler
from the outer context (themain
function). (AndRc
is obviously!Send
.)The only other way to run
LocalSet
that I know of is to callLocalSet::block_on
, but it requires aRuntime
which I don't think is possible to get from inside a context of#[tokio::main]
, and also creating a new Runtime is not possible because I'm already in a context of a runtime.Also, if I try to remove
await
and just pass theFuture
returned byLocalSet::run_until
totokio::spawn
like this:then the compiler panics: rust-lang/rust#68109
What I'm trying to achieve is to run Actix inside Tokio runtime in a separate thread so I could send it a shutdown signal at some point.
Do you think there is a way to run
LocalSet
inside Tokio-spawned task? Or am I getting the whole idea ofLocalSet
wrong? 😬Thanks!
The text was updated successfully, but these errors were encountered: