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

[cli] made cli multithreaded :3 #938

Merged
merged 2 commits into from
Nov 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 53 additions & 20 deletions src-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#![allow(dead_code)]
use std::{path::PathBuf, process::exit};
use std::{
path::PathBuf,
process::exit,
thread::{self, JoinHandle},
};

use choreo_core::{
file_management::{self, WritingResources},
Expand Down Expand Up @@ -170,6 +174,8 @@ impl Cli {
return;
}

let mut thread_handles: Vec<JoinHandle<()>> = Vec::new();

// generate trajectories
for (i, trajectory_name) in trajectory_names.iter().enumerate() {
tracing::info!(
Expand All @@ -183,33 +189,60 @@ impl Cli {
.await
.expect("Failed to read trajectory file");

match generate(project.clone(), trajectory, i as i64) {
Ok(new_trajectory) => {
match file_management::write_trajectory_file_immediately(
&resources,
new_trajectory,
)
.await
{
Ok(_) => {
tracing::info!(
"Successfully generated trajectory {:} for {:}",
trajectory_name,
project.name
let cln_project = project.clone();
let cln_resources = resources.clone();
let cln_trajectory_name = trajectory_name.clone();
let handle =
thread::spawn(
move || match generate(cln_project.clone(), trajectory, i as i64) {
Ok(new_trajectory) => {
let runtime =
choreo_core::tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.expect("Failed to build tokio runtime");
let write_result = runtime.block_on(
file_management::write_trajectory_file_immediately(
&cln_resources,
new_trajectory,
),
);
match write_result {
Ok(_) => {
tracing::info!(
"Successfully generated trajectory {:} for {:}",
cln_trajectory_name,
cln_project.name
);
}
Err(e) => {
tracing::error!(
"Failed to write trajectory {:} for {:}: {:}",
cln_trajectory_name,
cln_project.name,
e
);
}
}
}
Err(e) => {
tracing::error!(
"Failed to write trajectory {:} for {:}: {:}",
trajectory_name,
project.name,
"Failed to generate trajectory {:}: {:}",
cln_trajectory_name,
e
);
}
}
}
},
);

thread_handles.push(handle);
}

for handle in thread_handles {
match handle.join() {
Ok(_) => {}
Err(e) => {
tracing::error!("Failed to generate trajectory {:}: {:}", trajectory_name, e);
tracing::error!("Failed to join thread: {:?}", e);
}
}
}
Expand Down
Loading