please find below code
pub async fn content_create(&mut self, params: &contentCreateParams) -> Result<contentCreateResponse, Box<dyn Error + Send + Sync>> {
if !self.is_logged_in {
return Err("Not logged in".into());
}
let response = timeout(Duration::from_secs(10), async {
let mut aggregated_results: Vec<Map<String, Value>> = Vec::new();
let mut xml_response: Option<String> = None;
let cl_trid = format!("{}-content-create-{}", self.prefix, Utc::now().timestamp_millis());
let xml_template = r#" "#;
let batch_size = 200;
let mut interval = interval(Duration::from_micros(100));
loop {
interval.tick().await;
let mut tasks = Vec::new();
for _ in 0..batch_size {
let params_clone = params.clone();
let task = tokio::spawn(async move {
let res = self.write_request(&xml_template);
});
tasks.push(task);
}
for task in tasks {
if let Err(e) = task.await {
eprintln!("Task error: {:?}", e);
}
}
}
}).await;
}
}
here
let res = self.write_request(&xml_template);
pass self into inside tokio tokio::spawn
In Rust, when using tokio::spawn to spawn an asynchronous task, you often encounter issues with borrowing and ownership. Specifically, you cannot directly use self inside the spawned task because it requires the ‘static lifetime. To solve this, you can use an Arc (atomic reference-counted) and Mutex to safely share ownership of self across threads.