- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 List
执行下载的参数。
我将该列表的元素映射到 Deferred
执行下载;然后,forEach
列表的元素,我称之为 await
,但显然下载是按顺序执行的。
这是我的功能:
suspend fun syncFiles() = coroutineScope {
remoteRepository.requiredFiles()
.filter { localRepository.needToDownload( it.name, it.md5 ) }
.map { async { downloader( it ) } }
.forEach { deferredResult ->
when ( val result = deferredResult.await() ) {
is DownloadResult.Layout -> localRepository.storeLayout( result.content )
is DownloadResult.StringR -> localRepository.storeFile( result )
}
}
}
private val useCase = SyncUseCaseImpl.Factory(
mockk { // downloader
coEvery { this@mockk.invoke( any() ) } coAnswers { delay(1000 );any() }
},
...
).newInstance()
@Test
fun `syncFiles downloadConcurrently`() = runBlocking {
val requiredFilesCount = useCase.remoteRepository.requiredFiles().size
assert( requiredFilesCount ).isEqualTo( 3 )
val time = measureTimeMillis {
useCase.syncFiles()
}
assert( time ).isBetween( 1000, 1100 )
}
expected to be between:<1000L> and <1100L> but was:<3081L>
@Test // OK
fun test() = runBlocking {
val a = async { delay(1000 ) }
val b = async { delay(1000 ) }
val c = async { delay(1000 ) }
val time = measureTimeMillis {
a.await()
b.await()
c.await()
}
assert( time ).isBetween( 1000, 1100 )
}
@Test // OK
fun test() = runBlocking {
val wasteTime: suspend () -> Unit = { delay(1000 ) }
suspend fun wasteTimeConcurrently() = listOf( wasteTime, wasteTime, wasteTime )
.map { async { it() } }
.forEach { it.await() }
val time = measureTimeMillis {
wasteTimeConcurrently()
}
assert( time ).isBetween( 1000, 1100 )
}
最佳答案
问题出在 mockk
如果您查看 coAnswer
的代码函数你会发现这个(API.kt + InternalPlatformDsl.kt):
infix fun coAnswers(answer: suspend MockKAnswerScope<T, B>.(Call) -> T) = answers {
InternalPlatformDsl.runCoroutine {
answer(it)
}
}
runCoroutine
看起来像这样。
actual fun <T> runCoroutine(block: suspend () -> T): T {
return runBlocking {
block()
}
}
coAnswer
是一个非挂起函数,并以
runBlocking
开始一个新的协程.
val mock = mockk<Downloader> {
coEvery {
this@mockk.download()
} coAnswers {
delay(1000)
}
}
val a = async {
mock.download()
}
mockk
执行
coAnswer
-block (
delay()
),它启动一个人工协程作用域,执行给定的 block 并等待(阻塞当前线程:
runBlocking
)直到这个 block 完成。所以答案 block 只在
delay(1000)
之后返回已完成。
coAnswer
运行的所有协程依次执行。
关于concurrency - 协程:List 中的延迟操作按顺序运行。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53238428/
我正在尝试在我的项目中使用 Knockout Concurrency 插件,目前我正在摆弄示例代码,但我没有让它工作: https://github.com/AndersMalmgren/Knocko
我正在尝试使用 grunt 运行多个监视任务,但似乎无法运行。我正在使用 grunt concurrent,但它似乎只运行我指定的一部分任务,只是短暂停止。 这是我的 gruntfile 的片段: c
我有一个使用 Grunt 的 Ionic 项目,它是由 Yeoman 构建的。我设法将其配置为在运行 Fedora 22 的本地计算机上正常工作。 目前,我正在尝试在 Centos 7 服务器实例上配
关闭。这个问题需要debugging details .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 1年前关闭。 Improve this question Co
Go is a concurrent lang 这是什么意思? 这是否意味着它是 C/C++/Java.. 的替代品? 最佳答案 A concurrent language是一种具有并发语言结构的语言
我正在尝试使用 Kafka 实现一个事件溯源系统,但遇到了以下问题。在新用户注册期间,我想检查用户提供的用户名是否已被使用。但是,请考虑 2 个用户尝试同时注册提供相同用户名的情况。 根据我对 ES
我正在完成 golang 之旅并进行最后的练习,将网络爬虫更改为并行爬行而不是重复爬行 (http://tour.golang.org/#73)。我只更改了抓取功能。 var used = m
ruby 版本 2.5.3 当我输入 rails new upload_app 时,出现以下错误 错误如下 Traceback (most recent call last): 39: fro
func main() { jobs := []Job{job1, job2, job3} numOfJobs := len(jobs) resultsChan := make
我正在尝试在 Rust async-await(即将稳定)中同时(而不是按顺序)运行 futures 列表,直到它们中的任何一个解析为 true . 想象一下有一个 Vec ,以及为每个文件运行的 f
当我看到这段代码时出现了问题: private static volatile ConcurrentHashMap cMap = null; static { cMap = new Concu
刚在lab环境下安装dcos环境,在centos7 linux机器上尝试安装dcos客户端时得到 **[root@rmavmdock5 dcos]# bash install.sh . http://
为什么要为 Scala fork ForkJoinPool? 哪种实现方式和哪种情况更受欢迎? 最佳答案 scala 库拥有自己的 ForkJoinPool 副本的明显原因是 scala 必须在 1.
是的,我知道。关于 NSOperation 世界有很多问题和答案,但我仍然有一些疑问。我会尝试用两部分的问题来解释我的疑虑。它们相互关联。 在 SO 帖子中 nsoperationqueue-and-
我将 Play Framework 2.1.1 与一个生成 java.util.concurrent.Future 结果的外部 java 库一起使用。我使用的是 scala future 而不是 Ak
我们使用 Doug Lea 的并发库已有 8 年多了。出于向后兼容性的原因,我们的代码仅限于使用 Java 2 语言级别和 JDK 1.3 库。 现在我们正在开发一个主要的新版本,并最终能够使用 Ja
此问题涉及当 saga 数据保留在 Azure 表存储中时对 saga 数据的并发访问。它也是在 Prefer 的文档中找到的引用信息:http://docs.particular.net/nserv
我有一个创建锁的方法。 ReadWriteLock lock = new ReentrantReadWriteLock(); 然后我使用 Lock Interface 将该对象传递到一个方法中。 m
当我在 Mac OSX 命令行上的 python 中执行以下操作时: >>> from concurrent.futures import ProcessPoolExecutor 我明白了 Modul
我正在 listview 的线程池上创建异步任务。我正在通过 asynchtask 的 listarray 处理这些任务。当 fragment 被销毁时我必须删除这些任务,并且当我在销毁最后一个 fr
我是一名优秀的程序员,十分优秀!