发行:
我很难理解为什么我的记者演员没有根据我的生成器演员中的以下声明接收消息:
reporter <! Message input我的记者演员是这样的:
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参与者:
let reporterActor message =
match message |> box :?> Command with
| Start -> ()
| Message v -> printf "Reporting: %s" v
| Exit -> failwith "Kill this!"我仍然不明白什么时候应该使用邮箱参数,而不是什么时候应该依赖消息参数。
发布于 2017-02-10 00:53:18
区别在于actorOf和actorOf2的工作方式。
actorOf与派生一起创建一个参与者,作为系统根的子角色,它将处理传递给它的函数'Message -> unit的消息。
actorOf2与派生一起创建一个参与者,作为您传入的参与者的子角色,该子角色将使用传递的函数'Message -> unit处理消息。
你对记者演员的最初功能签名是:
Actor<'Message> -> unit你用了spawn system "reporterActor" (actorOf(reporterActor))
在本例中,您是说创建的新参与者将接收的消息类型将是Actor<'Message>类型。之所以这样编译,是因为actorof只需要一个接受“消息”的函数,而“消息”是通用的,因此Actor<'Message>满足“Message参数”。
当您更新reporterActor的签名时,您将签名更改为'Message -> unit,这正是actorOf实际上要接受的。
简而言之,泛型允许您的代码编译,因为‘消息不是真正的限制,也不应该是真正的限制。
来自:http://getakka.net/docs/FSharp%20API
actorOf
(fn : 'Message -> unit) (mailbox : Actor<'Message>) : Cont<'Message, 'Returned>--使用一个函数,它将消息作为唯一的参数。邮箱参数由产卵函数注入。actorOf2 (fn : Actor<'Message> -> 'Message -> unit) (mailbox : Actor<'Message>) : Cont<'Message, 'Returned>-使用一个函数,它同时接受消息和一个Actor实例作为参数。邮箱参数由产卵函数注入。示例:让handleMessage (邮箱:参与者<‘a>) msg =>将msg匹配为>x -> printf "%A“x”%x“-> () >>让aref =产卵系统”my-actorOf2“(actorOf2 handleMessage)让> blackHole =产卵系统”黑洞“(actorOf (有趣的msg -> () 派生(actorFactory : IActorRefFactory) (名称: string) (f :参与者<‘Message> -> Cont<'Message,'Returned>):IActorRef --使用指定的参与者计算表达式生成一个参与者。该参与者只能在本地使用。 所有这些功能都可以与参与者系统或参与者本身一起使用。在第一种情况下,生成的参与者将置于当前参与者系统层次结构的/user根监护人之下。在第二个选项中,派生的参与者将成为用作产卵函数的actorFactory参数的参与者的子变量。
https://stackoverflow.com/questions/42142299
复制相似问题