使用带有回复通道的MailboxProcessor创建按顺序返回值的有限代理



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

// 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

我的新方法,这段代码是由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

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

相关内容

  • 没有找到相关文章

最新更新