我正在玩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)不支持中断消息队列。
关于python - Pykka:如何在 Actor 停止时取消其待处理消息队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28113660/