Last active
October 22, 2021 22:58
-
-
Save dougluce/23941e88c87c3dde580a698bb44ba105 to your computer and use it in GitHub Desktop.
Two downloads at a time from a big list of downloads
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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