Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the most Pythonic way of processing messages like this Java "instance-filtering" [RabbitMQ]

Comming from a Java background, when developing services connected by JMS I used to process messages and distinguish them by checking their type, e.g (simplified):

  Object object = myQueue.consume();
   if (object instanceof MessageA) {
      processMessageA((MessageA) object)
   } else if (object instanceof MessageB) {
      processMessageB((MessageB) object)
   }...

So now I am building a messaging front-end for some Python modules in RabbitMQ (topic communication). I am planing on using one queue for each consumer-module to which different messages will arrive.

I have almost everything but I am still struggling with the processing (consuming) of messages. How would you distinguish between message type?

I thought of having custom JSON headers, but I don't know if this is correct.

like image 292
Jorge Avatar asked Apr 23 '20 14:04

Jorge


People also ask

What is the use of it () method in Java?

It is mainly used in the event handling. Let's see the example: In event handling (or) in a situation where we have to provide reference of a class to another one. It is used to reuse one object in many methods.

What is the use of this keyword in Java?

There can be a lot of usage of Java this keyword. In Java, this is a reference variable that refers to the current object. Here is given the 6 usage of java this keyword. this can be used to refer current class instance variable. this () can be used to invoke current class constructor. this can be passed as an argument in the method call.

How does isinstance() work in Python?

Notice that isinstance () takes two arguments, an object and a class. In the example above, isinstance () checks if miles is an instance of the Dog class and returns True. The miles, buddy, jack, and jim objects are all Dog instances, but miles is not a Bulldog instance, and jack is not a Dachshund instance:


1 Answers

When programming in python, especially for people who come from OO languages, it is important to keep two principles in mind.

First, Python is not an OO language, it only supports classes and objects. The most pythonic way is usually one that does not rely on typing and\or classes.

Second, there is the all-important "Zen of Python". This set of ideas dictates most of the way the Python language itself is built, but and also provide for those of us who program in it. Among these ideas there are two that you should strive to fulfill always.

  • Explicit is better than implicit.
  • Simple is better than complex.

Using these ideas I'll try to show what I believe the best way is, that way indeed uses JSON headers. Being explicit means that we should plainly state what we're trying to do, and being simple means that we should write it down where it is most logical for it to be. I would argue that this directly points at the type being in the JSON itself to be the most explicit and simple way of implementing this idea. Furthermore, I assume there is a difference between your message types, and not stating the type in the header would require you to write some code to distinguish between then based on some obscure difference, again defeating the concept of explicitness.

To close off, you should remember that anything in python is an object, including functions, which means that you can use a map as the operation dictionary:

message_to_action_map = {
    'typeA': functionA,
    'typeB': functionB
}

def consumer_callback(msg):
    # In Python, RabbitMQ works by push and not by pull
    process = message_to_action_map[msg['type']]
    process(msg)

This allows you to have one place where all your code paths are explicitly specified (this is also called the Strategy pattern). It also has the added bonus of you never having to change the actual processing code.

In short, I believe that using message headers is indeed the most pythonic way to tell the messages apart, as it is both simpler and more explicit that any other way.

like image 196
Ori Cohen Avatar answered Oct 24 '22 03:10

Ori Cohen