https://kotlinlang.org logo
#coroutines
Title
# coroutines
j

jkbbwr

01/07/2019, 7:57 PM
Copy code
val threadPool = newSingleThreadContext("git-clone-subprocess-pool")

suspend fun clone(urL: String) {
    val result = withContext(threadPool) {
        async {
            val pb = NuProcessBuilder(listOf("/bin/cat"))
            val process = pb.start()
            process.waitFor(0, TimeUnit.SECONDS)
        }
    }
    
    val exitcode = result.await()
}
a

Allan Wang

01/07/2019, 7:58 PM
If you have stuff between
async
and
await
then it's good. Otherwise just use
withContext
You should also be able to add context directly in
async
, though as you can see in the post above there are sometimes issues
j

jkbbwr

01/07/2019, 7:59 PM
Copy code
suspend fun clone(urL: String) {
    val exitcode = withContext(threadPool) {
        val pb = NuProcessBuilder(listOf("/bin/cat"))
        val process = pb.start()
        process.waitFor(0, TimeUnit.SECONDS)
    }
}
gotcha
a

Allan Wang

01/07/2019, 8:00 PM
Yeah. and assuming you want to return the exit code, add the return type to your clone function. Or just do
clone(url: String) = withContext(threadPool) ...
l

louiscad

01/07/2019, 10:23 PM
You can also use
<http://Dispatchers.IO|Dispatchers.IO>
for this kind of usage
3 Views