Skip to content
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

actix_rt::main is not compatible with tokio block_on #450

Closed
Guara92 opened this issue Mar 12, 2022 · 1 comment
Closed

actix_rt::main is not compatible with tokio block_on #450

Guara92 opened this issue Mar 12, 2022 · 1 comment

Comments

@Guara92
Copy link

Guara92 commented Mar 12, 2022

Trying to run async code from a sync function, with actix_rt::main the future never ending it's execution, with tokio::main it works without problems.

Example code:

use tokio::runtime::Handle;
use tokio::time::sleep;

#[actix_rt::main]
async fn main() {
    call_sync();
}

pub fn call_sync() {
    let handle = Handle::current();
    let five = std::thread::spawn(move || {
        handle.block_on(get_five_async())
    })
    .join()
    .unwrap();
    println!("{five:?}");
}

pub async fn get_five_async() -> i32 {
    sleep(core::time::Duration::from_millis(100)).await;
    5
}

I minimized the code where I actually have this problem, an actix-web endpoint where I'm trying to block waiting for a future to resolve, in the real code the future never runs even with tokio::main macro, I'm doing something wrong or there is a compatibility issue?

@Guara92 Guara92 changed the title actix_rt::main is not compatible with tokio block_on actix_rt::main is not compatible with tokio block_on Mar 12, 2022
@Guara92
Copy link
Author

Guara92 commented Mar 12, 2022

I'll answer for myself, unfortunately this is not possible in a single-threaded runtime like actix-rt, the same problem also occurs with #[tokio::main(flavor="current_thread")].

@Guara92 Guara92 closed this as completed Mar 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant