python - Pykka:如何在 Actor 停止时取消其待处理消息队列

标签 python concurrency actor pykka

我正在玩Pykka Actor 库和我想出了以下很棒的愚蠢示例:

import pykka
from time import sleep

class TestActor(pykka.ThreadingActor):
    def on_receive(self, message):
        sleep(1)
        print(message["v"])    

a = TestActor.start()
for i in xrange(10):
    print("asking for " + str(i))
    a.tell({"v":i})
print(a.stop())

我得到了预期的结果:立即打印 10 行请求行,并在 1 秒时间段内每行打印另外 10 行:

asking for 0
asking for 1
asking for 2
asking for 3
asking for 4
asking for 5
asking for 6
asking for 7
asking for 8
asking for 9
0
1
2
3
4
5
6
7
8
9
True

在参与者处理完所有请求后,True 将作为 stop 操作的结果打印出来。

我想知道是否可以阻止 actor 从而取消对重读消息的接收和处理。

I've checked the library documentation但我能找到的只是 block stop 参数,这意味着完全不同的事情:当设置为 False 时,它会使调用异步,但其与消息队列有关的行为是相同:

stop(block=True, timeout=None)

Send a message to the actor, asking it to stop.

Returns True if actor is stopped or was being stopped at the time of the call. False if actor was already dead. If block is False, it returns a future wrapping the result.

Messages sent to the actor before the actor is asked to stop will be processed normally before it stops.

Messages sent to the actor after the actor is asked to stop will be replied to with pykka.ActorDeadError after it stops.

The actor may not be restarted.

block and timeout works as for ask().

Returns: pykka.Future, or a boolean result if blocking

最佳答案

Pykka 目前(1.2.0)不支持中断消息队列。

来源:https://github.com/jodal/pykka/issues/46

关于python - Pykka:如何在 Actor 停止时取消其待处理消息队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28113660/

相关文章:

c++ - 具有(读/写)-only 内存顺序的内存排序或读-修改-写操作

java - libgdx 0.9.7 actor hit 不接收 actor 系统坐标

android - Scala Actor 线程控制

Scala final 与 val 的并发可见性

python - 机器人框架访问测试套件元数据中的关键字

python - AWS Lambda - SQS 与指数退避集成

python - 将 SHAP 瀑布图导出到数据框

java - 使用 Android 应用程序将文件上传到我的 Google API 项目

node.js - 如何重试mongodb事务?

Oracle - SELECT-INSERT 锁定模式