actor

How to properly use akka actors in scala

帅比萌擦擦* 提交于 2019-12-19 12:24:00
问题 I'm relatively new to the idea of actors, and was wondering if I could get some critique on what I am doing. For part of a project, I need to have an actor that tells a collection of listening actors the time. The listening actors must be able to be added to this actor. Currently I have this: import akka.actor.Actor; import akka.actor.ActorRef; import com.github.nscala_time.time.Imports._; class TimeManager extends Actor { var actors:List[ActorRef] = List(); def receive = { case AdvanceTime()

Akka(9): 分布式运算:Remoting-远程构建式

一世执手 提交于 2019-12-19 10:11:43
上篇我们讨论了Akka-Remoting。我们说Akka-Remoting是一种点对点的通讯方式,能使两个不同JVM上Akka-ActorSystem上的两个Actor之间可以相互沟通。Akka-Remoting还没有实现完全的Actor位置透明(location transparency),因为一个Actor还必须在获得对方Actor确切地址信息后才能启动与之沟通过程。Akka-Remoting支持“远程查找”和“远程构建”两种沟通方式。由于篇幅所限,我们只介绍了“远程查找”。在这一篇里我们将会讨论“远程构建”方式。 同样,我们先通过项目结构来分析: lazy val local = (project in file(".")) .settings(commonSettings) .settings( name := "remoteCreateDemo" ).aggregate(calculator,remote).dependsOn(calculator) lazy val calculator = (project in file("calculator")) .settings(commonSettings) .settings( name := "calculator" ) lazy val remote = (project in file("remote"))

How do I kill a RemoteActor?

淺唱寂寞╮ 提交于 2019-12-19 08:18:26
问题 Not sure whether I am missing something. When making an actor remote, the main method does not terminate. Here is a snippet that demonstrates the problem. import scala.actors._ import scala.actors.remote._ object TestMe { def main(args : Array[String]) : Unit = { object jim extends DaemonActor { // comment out these two lines and the application will terminate RemoteActor.alive(12345) RemoteActor.register('jim,this) def act { loop { receive { case 'quit => println("\nquiting") exit('normal)

Best method to peek into a Scala Actor's Mailbox

人走茶凉 提交于 2019-12-19 06:57:49
问题 Using Scala 2.8 RC1 or newer, what is the best (easiest and/or most direct) method to "peek" at the waiting messages in an actor's mailbox (from within the same actor's act() method) in order to examine what is in the queue, without having to react/receive the messages and/or disturb the current contents of the mailbox in any way. The purpose of this is so that an actor may determine if it is safe to process a request to exit by first determining if any of the remaining mailbox messages are

A3C

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-19 05:32:25
今天我们开始讲下A3C。 解决问题 收敛速度慢是之前Actor-Critic算法的问题。 对此提出三点改进: 1.还记得之前的参数更新公式: θ = θ + α ∇ θ l o g π θ ( S t , A ) δ \theta = \theta + \alpha \nabla_{\theta}log \pi_{\theta}(S_t,A)\delta θ = θ + α ∇ θ ​ l o g π θ ​ ( S t ​ , A ) δ A3C使用了另外的优势函数形式: A ( S , A , w , β ) = Q ( S , A , w , α , β ) − V ( S , w , α ) A(S,A,w,\beta) = Q(S,A, w, \alpha, \beta) - V(S,w,\alpha) A ( S , A , w , β ) = Q ( S , A , w , α , β ) − V ( S , w , α ) 参数更新形式: θ = θ + α ∇ θ l o g π θ ( s t , a t ) A ( S , A , w , β ) \theta = \theta + \alpha \nabla_{\theta}log \pi_{\theta}(s_t,a_t)A(S,A,w,\beta) θ = θ + α ∇ θ ​ l o g π θ ​

[Akka]发送一条消息的内部流程

Deadly 提交于 2019-12-19 01:50:28
本想通过了解一下Akka-actor工程中主要的类的概念,来看下Akka内部运作的机制。无奈里边的类的确太多,注释中对每个类的功能也没有足够的解释。所以还是通过debug的方式,找个入手点,看一下互相之间调用的关系。 最初的选择是看一下ActorSystem的实始化过程,但发现难度挺大,因为这个初始化工作是为以后的行为做准备,所以仅根据初始化的动作,难以了解其目的是什么。所以就试着从消息发送的过程了解起,发现这个逻辑更好理解。 下面来看一下其执行过程吧。代码如下,使用默认的配置。 object Main extends App{ val system = ActorSystem("football") val actor = system.actorOf(Props[PrintActor]) actor ! "hello" } 想要了解的是actor ! "hello"的执行过程。 首先,actor的类型是ActorRef,在默认的配置下,这个ActorRef的具体类型为RepointableActorRef。 它的!方法的定义为 def !(message: Any)(implicit sender: ActorRef = Actor.noSender) = underlying.sendMessage(message, sender) underlying的定义为 def

Akka actor logging not writing to file

半城伤御伤魂 提交于 2019-12-18 19:21:52
问题 I'm attempting to log to a file rather than stdout. My application.conf (in src/main/resources/): akka { event-handlers = ["akka.event.slf4j.Slf4jEventHandler"] loglevel = "DEBUG" } logback.xml (in src/main/resources/): <configuration> <appender name="FILE" class="ch.qos.logback.core.FileAppender"> <file>log/app.log</file> <append>true</append> <encoder> <pattern>%date{yyyy-MM-dd} %X{akkaTimestamp} %-5level[%thread] %logger{1} - %msg%n</pattern> </encoder> </appender> <root level="DEBUG">

Should my Scala actors' properties be marked @volatile?

﹥>﹥吖頭↗ 提交于 2019-12-18 16:27:18
问题 In Scala, if I have a simple class as follows: val calc = actor { var sum = 0 loop { react { case Add(n) => sum += n case RequestSum => sender ! sum } } } Should my field sum be marked @volatile ? Whilst the actor is logically single-threaded (i.e. the messages are processed sequentially), the individual reactions may be happening on separate threads and hence the state variable may be being altered on one thread and then read from another. 回答1: You don't need to mark them as volatile. The

Azure service fabric actor dependency injection

给你一囗甜甜゛ 提交于 2019-12-18 14:10:55
问题 Is there any way to inject dependencies in to the Azure Service Fabric Actor's constructor? 回答1: Having had a bit of a dig-around in this area with dotPeek a while back (trying to resolve actors from an Autofac lifetime scope per-invocation), I think the trick is to create your own implementation of StatelessActorServiceFactory, and your own extension method to register the actor with it. Although the factory class is marked as internal, its interface (IStatelessServiceFactory) and the

How to write a timer actor in Scala?

淺唱寂寞╮ 提交于 2019-12-18 07:36:46
问题 I need an actor to send a message every minute. How do I best achieve this behaviour? I am afraid of using java.lang.Thread.sleep(long millis) as a thread can be shared among many actors in Scala, as far as I understand. 回答1: Create an actor with receiveWithin to act as the timer. 回答2: Or as @Daniel mentioned, here a running example: import scala.actors._ import scala.actors.Actor._ class TimerActor(val timeout: Long,val who: Actor,val reply: Any) extends Actor { def act { loop { reactWithin