F# 为什么不是';我的演员没有收到消息吗?
问题:F# 为什么不是';我的演员没有收到消息吗?,f#,akka.net,F#,Akka.net,问题: let reporterActor (mailbox:Actor<_>) = let rec loop() = actor { let! msg = mailbox.Receive() match msg |> box :?> Command with | Start -> ()
let reporterActor (mailbox:Actor<_>) =
let rec loop() = actor { let! msg = mailbox.Receive()
match msg |> box :?> Command with
| Start -> ()
| Message v -> printf "%s" v
| Exit -> mailbox.Context.System.Terminate() |> ignore }
loop() |> ignore
module Main
open System
open Akka.FSharp
open Akka.Actor
open Actors
type Command =
| Message of string
| Start | Exit
let reporterActor (mailbox:Actor<_>) =
let rec loop() = actor { let! msg = mailbox.Receive()
match msg |> box :?> Command with
| Start -> ()
| Message v -> printf "%s" v
| Exit -> mailbox.Context.System.Terminate() |> ignore }
loop() |> ignore
let generatorActor (reporter:IActorRef) (mailbox:Actor<_>) message =
let handle input = match input with
| "exit" -> mailbox.Context.System.Terminate |> ignore
| _ -> reporter <! Message input
handle (Console.ReadLine().ToLower())
[<EntryPoint>]
let main argv =
let system = System.create "system" (Configuration.load())
let reporterActor = spawn system "reporterActor" (actorOf(reporterActor))
let generatorActor = spawn system "generatorActor" (actorOf2(generatorActor reporterActor))
generatorActor <! Start
system.AwaitTermination ()
0
我很难理解为什么我的记者演员没有收到基于我的生成器演员中的以下声明的消息:
reporter <! Message input
我仍然不明白什么时候应该使用邮箱参数,什么时候应该依赖消息参数。区别在于actorOf和actorOf2的工作方式 actorOf与spawn一起创建一个actor,作为系统根目录的子目录,该目录将使用传递给它的函数
'Message->unit
处理消息
actorOf2与spawn一起创建一个actor作为您传入的actor的子级,子级将使用传递的函数'Message->unit
处理消息
reporter actor的原始函数签名是:
Actor<'Message> -> unit
繁殖(actorFactory:IActorRefFactory)(名称:string)(f:
ActorTanks Adam。你也有测试演员的经验吗?我没有,但我发现这个链接可能对测试很有帮助。我不确定是否有F#化的库,但我相信你可以让它与F#一起工作。
Actor<'Message> -> unit
> let handleMessage (mailbox: Actor<'a>) msg =
> match msg with
> | Some x -> printf "%A" x
> | None -> ()
>
> let aref = spawn system "my-actor" (actorOf2 handleMessage) let
> blackHole = spawn system "black-hole" (actorOf (fun msg -> ()))