Skip to content

Instantly share code, notes, and snippets.

@dougluce
Last active October 22, 2021 22:58
Show Gist options
  • Save dougluce/23941e88c87c3dde580a698bb44ba105 to your computer and use it in GitHub Desktop.
Save dougluce/23941e88c87c3dde580a698bb44ba105 to your computer and use it in GitHub Desktop.
Two downloads at a time from a big list of downloads
import java.util.concurrent.Semaphore
// ...
const CONCURRENT_DOWNLOADS = 2
val queue = Sempahore(CONCURRENT_DOWNLOADS)
fun download(url: URL) {
// .. do the download
queue.release()
}
suspend fun gettem() = coroutineScope {
val list = repo.getList()
list.forEach {
queue.acquire()
launch {
download(it.url)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment