Search code examples
pythonconcurrencyactorpykka

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


I've playing with Pykka actors library and I came up with the following awesome silly example:

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())

I get the expected result: 10 asking for lines which are printed immediately and another 10 lines each printed in 1 second time period:

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

After all the requests have been served by the actor, True is printed as the result of the stop action.

I wonder if it would be possible to stop the actor thus cancelling the reception and processing of the reamining messages.

I've checked the library documentation but all I can find is the block stop parameter which means a quite different thing: When set to False it makes the call to be asynchronous but its behaviour concerning the message queue is the same:

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


Solution

  • Pykka does not currently (1.2.0) support interrupting the message queue.

    Source: https://github.com/jodal/pykka/issues/46