gpt4 book ai didi

kotlin - 异步构造生产者

转载 作者:行者123 更新时间:2023-12-02 12:53:03 25 4
gpt4 key购买 nike

我有一种情况,我想异步使用ReceiveChannel构造一个produce,但是它挂了。这是一个简化的示例:

runBlocking {
val deferredChannel = async {
produce<String> { send("foo") }
}

val channel = deferredChannel.await()

println("Got channel")

val value = channel.receive()

println("Got value $value")
}
println均未命中。可能存在某种协程僵局,但我不清楚在何处/如何进行。

如何异步生成 ReceiveChannel

编辑:如果我将 produce更改为 produce(capacity = 1),则可以使用,但是为什么呢?不管生产者的能力如何, await()至少不成功吗?如果我想保持容量= 0,该怎么办?

最佳答案

It works if I change produce to produce(capacity = 1), but why is that? Shouldn't the await() succeed, at least, regardless of the producer's capacity?



检查您调用的 produce()方法上的 docs,尤其是检查我们具有的容量参数和 Channel上的 docs(重点是我的):

When capacity is 0 – it creates RendezvousChannel. This channel does not have any buffer at all. An element is transferred from sender to receiver only when send and receive invocations meet in time (rendezvous), so send suspends until another coroutine invokes receive and receive suspends until another coroutine invokes send.



这可能是它挂起的原因。您正在 send线程上调用 async,然后为其调用 await ...但是,正如文档所说,尚无其他例程调用 receive ...,因此它将挂起直到发生这种情况,在这种情况下将挂起。

检查Channel上的同一链接,我们还看到为什么给它一个大于0的数字可以解决这个问题(强调我的意思):

When capacity is positive, but less than UNLIMITED – it creates array-based channel with given capacity. This channel has an array buffer of a fixed capacity. Sender suspends only when buffer is full and receiver suspends only when buffer is empty.

关于kotlin - 异步构造生产者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56431199/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com