简体   繁体   English

MailboxProcessor使用指南?

[英]MailboxProcessor usage guidelines?

I've just discovered the MailboxProcessor in F# and it's usage as a "state machine" ... but I can't find much on the recommended usage of them. 我刚刚在F#中发现了MailboxProcessor并且它被用作“状态机”......但我对它们的推荐用法找不到多少。

For example... say I'm making a simple game with 100 on-screen enemies should I use a MailboxProcessor to change enemy position and health; 例如...假设我正在制作一个有100个屏幕上敌人的简单游戏,我应该使用MailboxProcessor来改变敌人的位置和健康状况; giving me 200 active MailboxProcessor? 给我200个活跃的MailboxProcessor?

Is there any clever thread management going on under the hood? 引擎盖下是否有任何聪明的线程管理? should I try and limit the amount of active MailboxProcessor I have or can I keep banging them out willy-nilly? 我应该尝试限制我拥有的活动邮箱处理器的数量,还是可以不断地敲打它们?

Thanks in advance, 提前致谢,

JD. JD。

A MailboxProcessor for enemy simulation might look like this: 用于敌人模拟的MailboxProcessor可能如下所示:

MailboxProcessor.Start(fun inbox ->
async {
  while true do
    let! message = inbox.Receive()
    processMessage(message)
})

It does not consume a thread while it waits for a message to arrive ( let! message = line). 它在等待消息到达时不消耗线程( let! message = line)。 However, once message arrives it will consume a thread (on a thread pool). 但是,一旦消息到达,它将消耗一个线程(在线程池上)。 If you have a 100 mailbox processors that all receive a message simultaneously, they will all attempt to wake up and consume a thread. 如果您有100个邮箱处理器同时收到一条消息,它们都将尝试唤醒并使用一个线程。 Since here message processing is CPU bound, 100s of mailbox processors will all wake up and start spawning (thread pool) threads. 由于此处消息处理受CPU限制,因此100个邮箱处理器将全部唤醒并开始生成(线程池)线程。 This is not a great performance. 这不是一个很好的表现。

One situation mailbox processors excel in is the situation where there is a lot of concurrent clients all sending messages to one processor (imagine several parallel web crawlers all downloading pages and sinking results to a queue). 邮箱处理器擅长的一种情况是,有许多并发客户端都向一个处理器发送消息(想象几个并行的网络爬虫都下载页面并将结果下沉到队列中)。 On-screen enemies case appears different - it is many entities responding to a single source of messages (player movement/time ticks). 屏幕上的敌人案例看起来不同 - 它是许多实体响应单个消息来源(玩家移动/时间刻度)。

Another example where thousands of MailboxProcessors is a great solution is I/O bound MailboxProcessor: 数千个邮箱处理器是一个很好的解决方案的另一个例子是I / O绑定的MailboxProcessor:

MailboxProcessor.Start(fun inbox ->
async {
  while true do
    let! message = inbox.Receive()
    match message with
    |  ->
         do! AsyncWrite("something")
         let! response = AsyncResponse()
         ...
})

Here after receiving a message the agent very quickly yields a thread but still needs to maintain state across asynchronous operations. 在收到消息后,代理很快就会生成一个线程但仍需要跨异步操作维护状态。 This will scale very very well in practice - you can run thousands and thousands of such agents: this is a great way to write a web server. 这在实践中可以非常好地扩展 - 您可以运行成千上万的这样的代理:这是编写Web服务器的好方法。

As per 按照

http://blogs.msdn.com/b/dsyme/archive/2010/02/15/async-and-parallel-design-patterns-in-f-part-3-agents.aspx http://blogs.msdn.com/b/dsyme/archive/2010/02/15/async-and-parallel-design-patterns-in-f-part-3-agents.aspx

you can bang them out willy-nilly. 你可以毫不犹豫地敲打它们。 Try it! 试试吧! They use the ThreadPool. 他们使用ThreadPool。 I have not tried this for a real-time GUI game app, but I would not be surprised if this is 'good enough'. 我没有尝试过这个实时的GUI游戏应用程序,但如果这个“足够好”我也不会感到惊讶。

say I'm making a simple game with 100 on-screen enemies should I use a MailboxProcessor to change enemy position and health; 如果我使用邮箱处理器来改变敌人的位置和健康,我说我正在制作一个有100个屏幕上敌人的简单游戏; giving me 200 active MailboxProcessor? 给我200个活跃的MailboxProcessor?

I don't see any reason to try to use MailboxProcessor for that. 我认为没有任何理由尝试使用MailboxProcessor A serial loop is likely to be much simpler and faster. 串行循环可能更简单,更快捷。

Is there any clever thread management going on under the hood? 引擎盖下是否有任何聪明的线程管理?

Yes, lots. 是的,很多。 But is it designed for asynchronous concurrent programming (particularly scalable IO) and your program isn't really doing that. 但它是否适用于异步并发编程(特别是可扩展的IO),而您的程序并没有真正做到这一点。

should I try and limit the amount of active MailboxProcessor I have or can I keep banging them out willy-nilly? 我应该尝试限制我拥有的活动邮箱处理器的数量,还是可以不断地敲打它们?

You can bang them out willy-nilly but they are far from optimized and performance is much worse than serial code. 你可以毫不犹豫地敲打它们,但它们远没有优化,性能比串行代码差很多。

也许这个这个有用吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM