问题描述
演员中异步的await支持
我移植演员LIB阿卡到.NET()
我想补充的异步/的await 支持的在的我的演员。
这给了我一些问题,现在因为如果我使用默认的调度程序,等待的延续将运行W / O对于演员并发boundarys。
意,当演员正在处理的消息,因为这将导致两个线程访问行动者的内部状态在同一时间的延续可能运行
如果我能以某种方式安排的任务,以演员自己的邮箱,完成任务的邮箱中运行,这将解决这个问题。
公共类SomeActor:UntypedActor
{
保护覆盖的onReceive(对象消息)
{
如果(消息SomeMessage)
{
DoAsyncStuff();
}
} 私人异步无效DoAsyncStuff()
{
VAR解析度=等待东西..
//这个code会不尊重演员并发边界
//因为它可以在相同的时间的onReceive运行
Console.WriteLine(RES);
}
}
我对演员一个线程静态上下文,所以当演员正在执行,这种情况下被设置。
所以,我可以很容易地查找从任务调度的积极参与者邮箱。
沿着线的东西:
公共类ActorTaskScheduler:的TaskScheduler
{
保护覆盖无效QueueTask(任务任务)
{
VAR自= ActorCell.Current.Self;
self.Tell(新ActorTask(任务),ActorRef.NoSender);
}
而ActorTask消息可能由演员的系统消息处理程序进行处理。
到目前为止好。
我只是不知道下一步该怎么做。
可我只是写了当前的TaskScheduler?
那是线程静态的?
我只是想申请一个演员正在运行,按此调度,也不会影响code演员之外运行。
这是可能在所有应用自定义任务调度的具体行动而已?
The proper way to "set" the current scheduler is to queue a delegate to the scheduler you want. When the delegate executes, it will have that scheduler as "current".
Is it possible to do this another way? Because then you have an easier solution available.
I'm thinking you could just run each actor inside a ConcurrentExclusiveSchedulerPair.ExclusiveScheduler
. This would mean any actor code runs on a thread pool thread; it could be any thread pool thread, but the ExclusiveScheduler
will ensure only one part of actor code would run at a time.
This "lifts" the actor abstraction from threads to tasks, which is an approach I would recommend if you could do it. It relieves pressure on memory and the thread pool when you have async actors. Of course, things like thread statics can't be used then, so you have to use something like logical call context or a Current
value that is set by a custom SynchronizationContext
or TaskScheduler
.
这篇关于自定义的TaskScheduler,SynchronizationContext的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!