我正在尝试创建一个字符串列表,该列表在邮箱处理器的帮助下逐渐异步插入元素。但是我没有得到想要的输出。
我几乎遵循了https://fsharpforfunandprofit.com/posts/concurrency-actor-model/中的代码, 但是在我的情况下它似乎没有按预期工作。我的代码如下:
type TransactionQueue ={
queue : string list
} with
static member UpdateState (msg : string) (tq : TransactionQueue) =
{tq with queue = (msg :: tq.queue)}
static member Agent = MailboxProcessor.Start(fun inbox ->
let rec msgLoop (t : TransactionQueue) =
async{
let! msg = inbox.Receive()
let newT = TransactionQueue.UpdateState msg t
printfn "%A" newT
return! msgLoop newT
}
msgLoop {queue = []}
)
static member Add i = TransactionQueue.Agent.Post i
[<EntryPoint>]
let main argv =
// test in isolation
printfn "welcome to test"
let rec loop () =
let str = Console.ReadLine()
TransactionQueue.Add str
loop ()
loop ()
0
我不断得到的结果只是最新输入的列表,不保留状态。所以如果我输入“a”然后“b”然后“c”队列将只有值“c”而不是“a”;“b”;“c”
任何帮助或指示将不胜感激!