Skip to content

Add on_spawn configuration for Builder #3181

@malaire

Description

@malaire

Is your feature request related to a problem? Please describe.

For monitoring and tracking purposes I want to run some custom code when any spawned task is created/polled/dropped.

Describe the solution you'd like

I propose a configuration method on_spawn for Builder which would take a wrapper function to be applied to all spawned tasks.

For example following two examples would be equivalent:

(First example in playground, with example Wrapper)

fn main() {
    tokio::runtime::Builder::new_multi_thread()
        .enable_all()
        .build()
        .unwrap()
        .block_on(async {
            tokio::spawn(Wrapper::new(async {
                println!("Hello world")
            })).await.unwrap();
        });
}
fn main() {
    tokio::runtime::Builder::new_multi_thread()
        .enable_all()
        .on_spawn(Wrapper::new)
        .build()
        .unwrap()
        .block_on(async {
            tokio::spawn(async {
                println!("Hello world")
            }).await.unwrap();
        });
}

Describe alternatives you've considered

Alternative solution would be to apply this wrapper in all places where task is spawned. While this is possible for my own code, and also for crates like Hyper which offer configuration option like with_executor, this is not possible when using crates which do not offer such configuration option.

Doing this configuration for tokio runtime would be much better option.

Metadata

Metadata

Assignees

Labels

A-tokioArea: The main tokio crateC-feature-requestCategory: A feature request.M-runtimeModule: tokio/runtime

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions