Skip to content

Spawning a child thread that has access to state that outlives the parent route #1930

Discussion options

You must be logged in to vote

The answer to this is

#[post("/create/<job_name>", format = "application/json", data = "<job_data>")]
pub async fn create_job(job_name: &str, data:  Data<'_>, some_state: &State<Arc<SomeStruct>){
   let parsed_data = parse_data(data);
   let state_clone = some_state.inner().clone()
   tokio::spawn(async move {
       state_clone.mutate(parsed_data, job_name);
   })
}

i.e. using inner to access the Arc sans the State to extend the lifetime beyond '__r

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by deckard-uni
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant