Multi-threading of Jobs
Opened this issue · 0 comments
abhi3700 commented
This is my code:
#[tokio::main(flavor = "multi_thread")]
async fn main() -> eyre::Result<()> {
// ... existing code ...
// Create a shared JobScheduler instance
let sched = JobScheduler::new().await?;
let sched = Arc::new(sched);
// Add a job that runs every 10 seconds
sched
.add(Job::new("1/10 * * * * *", |_uuid, _l| {
println!("I run every 10 seconds");
})?)
.await?;
// Add an async job that runs every 1 minute
sched
.add(Job::new_async("every 1 minutes", move |_uuid, _l| {
Box::pin(async move {
println!("I run every 1 minutes");
let _ = perform_task_1().await;
})
})?)
.await?;
// Start the scheduler in a separate task
tokio::spawn(async move {
tracing::info!("Starting scheduler");
let _ = sched.start().await;
});
// ... existing code ...
}
Here, I am starting the scheduler only once for 2 scheduled jobs. And using tokio::spawn()
only once.
Can someone confirm if my jobs would run in separate threads each time?