- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
actors 中的异步等待支持
我正在将 actor 库 Akka 移植到 .NET ( https://github.com/rogeralsing/Pigeon )我想在我的 Actor 内部添加async/await支持。
这现在给我带来了一些问题,因为如果我使用默认调度程序,等待延续将在不考虑参与者并发边界的情况下运行。这意味着,延续可能会在 actor 处理消息时运行,因为这会导致两个线程同时访问 actor 的内部状态。
如果我能以某种方式将任务安排到 Actor 自己的邮箱,并在邮箱运行中完成任务,这将解决问题。
public class SomeActor : UntypedActor
{
protected override OnReceive(object message)
{
if (message is SomeMessage)
{
DoAsyncStuff();
}
}
private async void DoAsyncStuff()
{
var res = await something..
//this code will not respect the actor concurrency boundary
//since it can run at the same time as OnReceive
Console.WriteLine(res);
}
}
我确实有一个 actor 的线程静态上下文,所以当 actor 执行时,这个上下文被设置。所以我可以很容易地从任务调度程序中查找活跃的参与者邮箱。类似的东西:
public class ActorTaskScheduler : TaskScheduler
{
protected override void QueueTask(Task task)
{
var self = ActorCell.Current.Self;
self.Tell(new ActorTask(task), ActorRef.NoSender);
}
并且 ActorTask 消息可以由 actor 的系统消息处理程序处理。到目前为止一切顺利。
我只是不知道下一步该做什么。我可以直接覆盖当前的 TaskScheduler 吗?该线程是静态的吗?我只想在 actor 运行时应用此调度程序,它可能不会影响在 actor 外部运行的代码。
是否可以仅针对特定操作应用自定义任务调度程序?
最佳答案
Can I just over write the current TaskScheduler?
“设置”当前调度程序的正确方法是将委托(delegate)排队到您想要的调度程序。当委托(delegate)执行时,它将把该调度程序作为“当前”。
I do have a thread static context for the actor, so when the actor is executing, this context is set.
是否可以用其他方式做到这一点?因为这样您就有了更简单的解决方案。
我认为您可以在 ConcurrentExclusiveSchedulerPair.ExclusiveScheduler
中运行每个 actor。这意味着任何参与者代码都在线程池线程上运行;它可以是任何线程池线程,但ExclusiveScheduler
将确保一次只有一部分参与者代码运行。
这将 actor 抽象从线程“提升”到任务,如果可以的话,我会推荐这种方法。当你有异步 actor 时,它可以减轻内存和线程池的压力。当然,那时不能使用诸如线程静态之类的东西,因此您必须使用诸如逻辑调用上下文之类的东西或由自定义 SynchronizationContext
设置的 Current
值或任务调度器
。
关于c# - 自定义 TaskScheduler、SynchronizationContext?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21965772/
我仍在学习整个任务概念和 TPL。根据我目前的理解,await 使用 SynchronizationContext 函数(如果存在)将任务分派(dispatch)到“某处”。另一方面,Task 类中的
感谢 Jeremy Miller 在 Functional Programming For Everyday .NET Development 中的出色工作,我有一个工作命令执行器,它可以做我想做的一
我使用 SynchronizationContext从 UI 线程上的后台线程调用方法。我读了this post它要求 Send 的差异和 Post .我明白答案并会调用Send每当我需要调用方法的结
我遇到了挂起等待的问题(描述 here )。在研究过程中,我发现调用 SetResult在我的 TaskCompletionSource实际上在调用 SetResult 的线程的上下文中调用等待继续(
public static void Init() { //var task = GetSource1(); //var task = GetSource2(); //var
在异步函数内部使用与外部不同的 SynchronizationContext 时,我的行为令人困惑。 我的大部分程序代码都使用自定义 SynchronizationContext,它只是将 SendO
我正在尝试了解 SynchronizationContext 和 friend 。如果我在例如开始时设置自定义同步上下文控制台应用程序。在什么情况下当前同步上下文将与我的异步操作一起流动?Task 和
我想了解更多关于 SynchronizationContext 的信息,所以我制作了这个简单的控制台应用程序: private static void Main() { var sc = ne
我不知道如何获取给定 Thread 的 SynchronizationContext: Thread uiThread = UIConfiguration.UIThread; Synchronizat
如何比较 SynchronizationContext?似乎同一个 Dispatcher 在使用 BeginInvoke 时可以创建不同的 SynchronizationContext。当我深入研究两
在Programming C#一书中,它有一些关于SynchronizationContext的示例代码。 : SynchronizationContext originalContext = Syn
对于 WinForms 和 WPF,我使用 SynchronizationContext 作为同步到 GUI 线程的方法。最近我遇到了旧式异步回调的问题: private void Button
我有以下代码: [TestMethod] public void StartWorkInFirstThread() { if (SynchronizationContext.Current =
SynchronizationContext 有一个名为 IsWaitNotificationRequired 的属性.出于其目的,docs有话要说: Determines if wait notif
说,我有以下代码: IPrincipal capturedPrincipal = Thread.CurrentPrincipal; myseq.AsParallel().Select(x => {
这是一个非常基本的问题,我想是的,但是我找不到任何明确的答案。 SynchronizationContext.Post()线程安全吗? 我有一个成员变量,它保存主线程的上下文,并且从多个线程中调用_c
SynchronizationContext.Current 在主线程上为 null,我很难弄清楚这一点。 static class Program { /// //
我正在创建 SynchronizationContext 的 STA 版本以用于 Windows Workflow 4.0。我想知道在发布回调时如何处理异常。 SynchronizationConte
简而言之,我已经实现了一个派生自 SynchronizationContext 的类,以便 GUI 应用程序可以轻松地使用在 GUI 线程以外的线程上引发的事件。我非常感谢对我的实现的评论。具体来说,
我有两个线程,一个是主线程,另一个是我创建的线程来做一些工作。我不明白当我从另一个线程调用 originalContext.Post(主线程的 SyncronizationContext)时会发生什么
我是一名优秀的程序员,十分优秀!