gpt4 book ai didi

multithreading - 使用带有回复 channel 的 MailboxProcessor 来创建按顺序返回值的有限代理

转载 作者:行者123 更新时间:2023-12-04 06:36:46 25 4
gpt4 key购买 nike

基本上,我想将以下内容更改为有限的线程解决方案,因为在我的情况下,计算列表太大,产生了太多线程,我想用更少的线程进行实验和测量性能。

// the trivial approach (and largely my current situation)
let doWork() =
[1 .. 10]
|> List.map (fun i -> async {
do! Async.Sleep (100 * i) // longest thread will run 1 sec
return i * i // some complex calculation returning a certain type
})
|> Async.Parallel
|> Async.RunSynchronously // works, total wall time 1s

我的新方法,此代码借用/启发 this online snippet from Tomas Petricek (我测试过,它有效,但我需要它返回一个值,而不是单位)。
type LimitAgentMessage = 
| Start of Async<int> * AsyncReplyChannel<int>
| Finished

let threadingLimitAgent limit = MailboxProcessor.Start(fun inbox -> async {

let queue = System.Collections.Generic.Queue<_>()
let count = ref 0
while true do
let! msg = inbox.Receive()
match msg with
| Start (work, reply) -> queue.Enqueue((work, reply))
| Finished -> decr count
if count.Value < limit && queue.Count > 0 then
incr count
let work, reply = queue.Dequeue()
// Start it in a thread pool (on background)
Async.Start(async {
let! x = work
do! async {reply.Reply x }
inbox.Post(Finished)
})
})


// given a synchronous list of tasks, run each task asynchronously,
// return calculated values in original order
let worker lst =
// this doesn't work as expected, it waits for each reply
let agent = threadingLimitAgent 10
lst
|> List.map(fun x ->
agent.PostAndReply(
fun replyChannel -> Start(x, replyChannel)))

现在,有了这个,原始代码将变为:
let doWork() = 
[1 .. 10]
|> List.map (fun i -> async {
do! Async.Sleep (100 * i) // longest thread will run 1 sec
return i * i // some complex calculation returning a certain type
})
|> worker // worker is not working (correct output, runs 5.5s)

总而言之,输出是正确的(它确实计算并传播回回复),但它在(有限集)线程中没有这样做。

我一直在玩,但认为我错过了明显的东西(此外,谁知道,有人可能喜欢有限线程邮箱处理器的想法,它可以按顺序返回其计算)。

最佳答案

问题是对 agent.PostAndReply 的调用. PostAndReply将阻塞直到工作完成。在 List.map 内部调用它将导致工作按顺序执行。一种解决方案是使用 PostAndAsyncReply它不会阻塞,还会返回一个异步句柄以获取结果。

let worker lst = 
let agent = threadingLimitAgent 10
lst
|> List.map(fun x ->
agent.PostAndAsyncReply(
fun replyChannel -> Start(x, replyChannel)))
|> Async.Parallel

let doWork() =
[1 .. 10]
|> List.map (fun i -> async {
do! Async.Sleep (100 * i)
return i * i
})
|> worker
|> Async.RunSynchronously

这当然只是一种可能的解决方案(取回所有异步句柄并并行等待它们)。

关于multithreading - 使用带有回复 channel 的 MailboxProcessor 来创建按顺序返回值的有限代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39318745/

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