Pykka:如何在停止时取消演员的待处理消息队列

Pykka: How to cancel an actor's pending messages queue when it has been stopped

我玩过 Pykka actors 库,我想出了以下很棒的愚蠢示例:

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 要求 行立即打印,另外 10 行每行在 1 秒内打印:

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

在所有请求都被 actor 处理后,True 被打印为 stop 动作的结果。

不知是否可以停止actor从而取消对追加消息的接收和处理

I've checked the library documentation 但我能找到的只是 block 停止参数,这意味着完全不同的事情:当设置为 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