
import pykka


class PlainActor(pykka.ThreadingActor):
    def __init__(self):
        super().__init__()
        self.stored_messages = []

    def on_receive(self, message):
        if message.get('command') == 'get_messages':
            return self.stored_messages
        else:
            self.stored_messages.append(message)


if __name__ == '__main__':
    actor = PlainActor.start()
    actor.tell({'no': 'Norway', 'se': 'Sweden'})
    actor.tell({'a': 3, 'b': 4, 'c': 5})
    print(actor.ask({'command': 'get_messages'}))
    actor.stop()


import pykka


GetMessages = object()


class PlainActor(pykka.ThreadingActor):
    def __init__(self):
        super().__init__()
        self.stored_messages = []

    def on_receive(self, message):
        if message is GetMessages:
            return self.stored_messages
        else:
            self.stored_messages.append(message)


if __name__ == '__main__':
    actor = PlainActor.start()
    actor.tell({'no': 'Norway', 'se': 'Sweden'})
    actor.tell({'a': 3, 'b': 4, 'c': 5})
    print(actor.ask(GetMessages))
    actor.stop()

Use custom message instead of dict
examples: Use custom message instead of dict

  
  import pykka
+ 
+ 
+ GetMessages = object()
  
  
  class PlainActor(pykka.ThreadingActor):
      def __init__(self):
          super().__init__()
          self.stored_messages = []
  
      def on_receive(self, message):
-         if message.get('command') == 'get_messages':
+         if message is GetMessages:
              return self.stored_messages
          else:
              self.stored_messages.append(message)
  
  
  if __name__ == '__main__':
      actor = PlainActor.start()
      actor.tell({'no': 'Norway', 'se': 'Sweden'})
      actor.tell({'a': 3, 'b': 4, 'c': 5})
-     print(actor.ask({'command': 'get_messages'}))
+     print(actor.ask(GetMessages))
      actor.stop()
  
Use custom message instead of dict
## Code Before:

import pykka


class PlainActor(pykka.ThreadingActor):
    def __init__(self):
        super().__init__()
        self.stored_messages = []

    def on_receive(self, message):
        if message.get('command') == 'get_messages':
            return self.stored_messages
        else:
            self.stored_messages.append(message)


if __name__ == '__main__':
    actor = PlainActor.start()
    actor.tell({'no': 'Norway', 'se': 'Sweden'})
    actor.tell({'a': 3, 'b': 4, 'c': 5})
    print(actor.ask({'command': 'get_messages'}))
    actor.stop()

## Instruction:
Use custom message instead of dict
## Code After:

import pykka


GetMessages = object()


class PlainActor(pykka.ThreadingActor):
    def __init__(self):
        super().__init__()
        self.stored_messages = []

    def on_receive(self, message):
        if message is GetMessages:
            return self.stored_messages
        else:
            self.stored_messages.append(message)


if __name__ == '__main__':
    actor = PlainActor.start()
    actor.tell({'no': 'Norway', 'se': 'Sweden'})
    actor.tell({'a': 3, 'b': 4, 'c': 5})
    print(actor.ask(GetMessages))
    actor.stop()
