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

Wait until all task done #2053

Closed
LinkTed opened this issue Jan 5, 2020 · 3 comments
Closed

Wait until all task done #2053

LinkTed opened this issue Jan 5, 2020 · 3 comments
Labels
A-tokio Area: The main tokio crate C-feature-request Category: A feature request. M-process Module: tokio/process

Comments

@LinkTed
Copy link
Contributor

LinkTed commented Jan 5, 2020

I used the #[tokio::main] macro. But how to implemented that the program waits until all task in the runtime are done?

#[tokio::main]
async fn main() {
    ...
}
@vorot93
Copy link
Member

vorot93 commented Jan 6, 2020

If you spawn all your needed tasks in main, you can easily do it with futures::future::join_all.

#[tokio::main]
async fn main() {
    let mut handles = vec![];
    handles.push(tokio::spawn(async { ... }));
    handles.push(tokio::spawn(async { ... }));

    futures::future::join_all(handles).await;
}

@LinkTed
Copy link
Contributor Author

LinkTed commented Jan 7, 2020

I cannot implement it like that because I spawn inside of another task.

#[tokio::main]
async fn main() {
    let mut handles = vec![];
    handles.push(tokio::spawn(async {
        tokio::spawn(async {...});
    }));
    handles.push(tokio::spawn(async { ... }));

    futures::future::join_all(handles).await;
}

Is there no function like shutdown_on_idle from the previous version?

@Gowee
Copy link
Contributor

Gowee commented Jan 16, 2020

See #1957 .

@LinkTed LinkTed closed this as completed May 10, 2020
@Darksonn Darksonn added A-tokio Area: The main tokio crate C-feature-request Category: A feature request. M-process Module: tokio/process labels May 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate C-feature-request Category: A feature request. M-process Module: tokio/process
Projects
None yet
Development

No branches or pull requests

4 participants