gpt4 book ai didi

haskell - 如何实现与 invokeAll 等效的 Haskell

转载 作者:行者123 更新时间:2023-12-02 21:10:38 26 4
gpt4 key购买 nike

我正在寻找与 ExecutorService.invokeAll 等效的功能在 haskell 。我需要实现它吗?来自文档:

Executes the given tasks, returning a list of Futures holding their status and results when all complete.

在我的用例中,任务大部分时间都在等待 IO,因此我只需要避免不断阻塞主线程,这会构建 Either 结果或错误的集合。

最佳答案

此类问题的标准解决方案是 "async"图书馆。据我了解您的问题,您需要这样的东西:

import Control.Concurrent.Async

processActions :: [IO a] -> IO ()
processActions actions = do
-- Execute all the actions concurrently and
-- get a list of asyncs (an equivalent to futures):
asyncs <- mapM async actions
-- Do whatever you want...
-- ...
-- Block until all the asyncs complete:
mapM_ wait asyncs

但是该库提供了许多其他模式,因此您应该检查一下。您可能需要的是 mapConcurrently功能。

关于haskell - 如何实现与 invokeAll 等效的 Haskell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22993929/

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