在Azure Service Fabric actor中启动线程? [英] Start a thread inside an Azure Service Fabric actor?

查看:101
本文介绍了在Azure Service Fabric actor中启动线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道Service Fabric中的参与者是单线程的。但是,假设我在actor方法中启动了一个新线程,那么会发生什么?即使

I know that actors inside Service Fabric are single-threaded. But suppose I start a new thread inside an actor method, what will happen then? Will an actor be deactivated even though the spawned thread is still executing?

/ azure / service-fabric / service-fabric-reliable-actors-lifecycle rel = nofollow noreferrer>文档,当一段时间未使用某个actor时,该actor将被停用。在这种情况下,已使用是指:

According to the documentation, an actor is deactivated when it has not been 'used' for some time. 'Used' in this context means either:


  • 接听电话

  • IRemindable.ReceiveReminderAsync 被调用

  • receiving a call
  • IRemindable.ReceiveReminderAsync being invoked

因此似乎没有考虑我启动的新线程。

So it seems that the new thread I started is not taken into account. But maybe someone can confirm this?

推荐答案

完全正确。

何时您启动一个新线程后,原始线程(actor)将继续运行并超出范围,而跨接线程将继续运行。看看接下来会发生什么:

When you start a new thread, the original thread(The actor) will continue running and go out of scope, and the spanned thread continue running. look what happens then:

当一个actor收到一个调用时,处理该调用的线程将使用SemaphoreSlim获取一个锁,以处理actor对象(如果另一个线程已经存在)。

When an actor receive a call, the thread handling this call will acquire a lock using a SemaphoreSlim to handle the actor object, if another thread has already acquired the lock, the current thread will wait for its release, so that it can continue once free.

一旦获取了锁,线程将执行并从锁中返回。方法被调用,它们释放锁定以使下一个线程继续执行。

Once the lock is acquired, the thread will execute and return from the method called, and them release the lock for the following thread to continue.

当您将新线程作为actor逻辑的一部分使用时,它将作为服务过程,但是这里的问题是,一旦离开方法范围,您将不再拥有对该线程的控制,但是它将继续运行,并且对于actor运行时任务已完成,下一个actor调用将创建

When you span a new thread as part of the actor logic, it will just run as part of the service process, but the problem here is that once you leave the method scope, you won't have control of this thread anymore, but it will keep running, and for the actor runtime the task has finished, the next actor call will create another thread, and the things will keep going.

问题将在以下时间开始:

The problem will start when:


  • 您无法控制正在运行的线程数,您的服务由于开始占用太多内存,SF可能会尝试在各个实例之间平衡actor的服务,因为它不了解这些线程,如果它移动actor服务,则所有线程将被中止,并且您将丢失这些操作。

  • 上一次调用的扩展线程将与新线程竞争以进行下一个actor调用。

  • 如果新线程使用actor数据来继续其他操作跨线程和参与者线程都将面临并发问题,在没有例外的情况下,您将具有无法轻松调查的奇怪行为。例如,一个线程更改了另一个线程正在使用的值。

  • 以及您可能会遇到的许多其他并发问题

  • You don't have control how many threads are running, your services will start consuming too much memory and SF might try to balance the actors\services across instances, because it does not know about these threads, if it move the actor service, all you threads will be aborted and you loose these operations.
  • The spanned thread from previous call will compete with the new thread for the next actor call.
  • If the new thread uses the actor data to continue other operation both, the spanned thread and the actor thread will face concurrency issues, in cases where no exception happens you will have strange behaviors where you can't investigate easily. For example, one thread changing a value being used by the other.
  • And many other concurrency issues you might face

在可能(需要)另一个线程的情况下,您可以:

In scenarios where you might(think) need another thread you could:


  • 创建另一个参与者来处理任务

  • 在队列中创建一条消息,以供其他服务处理

  • 将任务作为参与者调用的一部分来执行。

这篇关于在Azure Service Fabric actor中启动线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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