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

Use tokio::sync::Semaphore to wait for available task slots #388

Closed
tfeda opened this issue Oct 19, 2022 · 1 comment
Closed

Use tokio::sync::Semaphore to wait for available task slots #388

tfeda opened this issue Oct 19, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@tfeda
Copy link
Contributor

tfeda commented Oct 19, 2022

Is your feature request related to a problem or challenge? Please describe what you are trying to do.

@Dandandan, I think we could improve on #378 by using tokio::sync::Semaphore to wait for task slots to open up, rather than sleep spinning. It could look something like this:

let available_task_slots = Arc::new(Semaphore::new(num_task_slots))

loop {
   // thread sleeps until permits are available from spawned tasks
   let task_permit = available_task_slots.acquire_owned().await  
    
   // poll scheduler for work

  if work_is_available {
     tokio::spawn(async move {
       // task work  
   
       // move permit into task scope
       drop(task_permit)
     })
  }
  // If no work is available, task_permit gets dropped here
}

Looking at the current implementation, its pretty much the same thing as what a tokio semaphore does, except with the sleeping spin loop.

Describe alternatives you've considered

Just a thought, implementing this isn't a deal breaker

@tfeda tfeda added the enhancement New feature or request label Oct 19, 2022
@Dandandan
Copy link
Contributor

@tfeda thanks... that seems what we are looking for.
Could save some extra milliseconds between tasks 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants