使用Akka Actor遍历目录树 [英] Use akka actors to traverse directory tree

查看:123
本文介绍了使用Akka Actor遍历目录树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是演员模型的新手,正试图写一个简单的例子。我想使用Scala和Akka遍历目录树。该程序应该找到所有文件,并对每个文件执行任意(但快速)操作。

I'm new to the actor model and was trying to write a simple example. I want to traverse a directory tree using Scala and Akka. The program should find all files and perform an arbitrary (but fast) operation on each file.

我想检查如何使用actor为递归建模?
遍历完成后,如何优雅地停止actor系统?
如何控制角色数量以防止内存不足?
有没有办法阻止演员的邮箱变得太大?
如果文件操作需要很长的时间才能执行?

I wanted to check how can I model recursion using actors? How do I gracefully stop the actor system when the traversal will be finished? How can I control the number of actors to protect against out of memory? Is there a way to keep the mailboxes of the actors from growing too big? What will be different if the file operation will take long time to execute?

任何帮助将不胜感激!

推荐答案


  1. 演员是工人。他们接受工作并提供结果,或者监督其他工人。通常,您希望角色有一个职责。

  2. 理论上,您可以让一个角色处理目录的内容,处理每个文件或为每个目录生成一个角色遇到。这很不好,因为很长的文件处理时间会使系统停滞。

  3. 有几种方法可以正常停止actor系统。 Akka文档中提到了其中的几个。

  4. 您可以有一个演员主管来排队对演员的请求,如果演员阈值以下,则生成演员,并在演员结束时减少该数量。这是主管演员的工作。主管演员可以在监视时坐在一边,也可以分派工作。 Akka的参与者模型实现了这两种方法的实现。

  5. 是的,有几种方法可以控制邮箱的大小。阅读文档。

  6. 如果操作错误,文件操作可能会阻止其他处理,例如幼稚的递归遍历。

  1. Actors are workers. They take work in and give results back, or they supervise other workers. In general, you want your actors to have a single responsibility.
  2. In theory, you could have an actor that processes a directory's contents, working on each file, or spawning an actor for each directory encountered. This would be bad, as long file-processing time would stall the system.
  3. There are several methods for stopping the actor system gracefully. The Akka documentation mentions several of them.
  4. You could have an actor supervisor that queues up requests for actors, spawns actors if below an actor threshold count, and decrementing the count when actors finish up. This is the job of a supervisor actor. The supervisor actor could sit to one side while it monitors, or it could also dispatch work. Akka has actor models the implement both of these approaches.
  5. Yes, there are several ways to control the size of a mailbox. Read the documentation.
  6. The file operation can block other processing if you do it the wrong way, such as a naive, recursive traversal.

要注意的第一件事是有两种类型的工作:遍历文件层次结构和处理单个文件。在您的第一个实现中,尝试创建两个参与者,参与者A和参与者B。参与者A将遍历文件系统,并向消息参与者B发送消息以及要处理的文件的路径。当参与者A完成时,它将所有完成指示符发送给参与者B并终止。当参与者B处理所有完成指示符时,它终止。这是一个基本的实现,您可以用来学习如何使用参与者。

The first thing to note is there are two types of work: traversing the file hierarchy and processing an individual file. As your first implementation try, create two actors, actor A and actor B. Actor A will traverse the file system, and send messages to actor B with the path to files to process. When actor A is done, it sends an "all done" indicator to actor B and terminates. When actor B processes the "all done" indicator, it terminates. This is a basic implementation that you can use to learn how to use the actors.

其他所有内容都是此变化。下一个变化可能是使用共享邮箱创建两个演员B。关机有点复杂,但仍然很简单。下一步是创建一个调度员演员,将工作分配给一个或多个演员B。下一个变体使用多个角色A遍历文件系统,并使用主管来控制创建多少个角色。

Everything else is a variation on this. Next variation might be creating two actor B's with a shared mailbox. Shutdown is a little more involved but still straightforward. The next variation is to create a dispatcher actor which farms out work to one or more actor B's. The next variation uses multiple actor A's to traverse the file system, with a supervisor to control how many actors get created.

如果遵循此开发计划,您将了解到有关如何使用Akka的很多知识,并且可以回答您的所有问题。

If you follow this development plan, you will have learned a lot about how to use Akka, and can answer all of your questions.

这篇关于使用Akka Actor遍历目录树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆