Get started with RabbitMQ, a message broker, that enables multiple processes to communicate reliably through an intermediary.
This project requires some free code - beyond that available in the Python Standard Library. To avoid messing up our local default Python installation, and any other Python projects we may have, we create a local virtual environment to install and use these libraries.
Think of a virtual environment as a safe sandbox. We can install whatever we want in our sandbox, and it won't break other Python projects that may require different versions, etc.
We use the built-in Python utility venv
to create our virtual environment.
There are other options, but this is simplest and most common.
We create the environment as a subfolder of this repo named .venv to keep it away from our project code.
- Git
- Python 3.7+ (3.11+ preferred)
- VS Code Editor
- VS Code Extension: Python (by Microsoft)
- RabbitMQ Server installed and running locally
- Fork this starter repo into your GitHub account.
- Clone your repo down to your machine.
- Explore your new project repo in VS Code on your local machine.
We will create a local Python virtual environment to isolate our project's third-party dependencies from other projects.
- Open a terminal window in VS Code.
- Use the built-in Python utility venv to create a new virtual environment named
.venv
in the current directory.
python -m venv .venv
Verify you get a new .venv directory in your project. We use .venv as the name to keep it away from our project files.
In the same VS Code terminal window, activate the virtual environment.
- On Windows, run:
.venv\Scripts\activate
- On Linux/MacOS, run:
source .venv/bin/activate
Verify you see the virtual environment name (.venv) in your terminal prompt.
To work with RabbitMQ, we need to install the pika library. A library is a collection of code that we can use in our own code. Learning to use free libraries that others have written to make our projects easier, faster, more reliable is a key skill for a developer.
We keep the list of third-party libraries needed in a file named requirements.txt. Use the pip utility to install the libraries listed in requirements.txt into our active virtual environment.
Make sure you can see the .venv name in your terminal prompt before running this command.
python -m pip install -r requirements.txt
In your VS Code terminal window, run the following commands to help verify your setup. These util files MAY be helpful to ensure you're setup correctly. You may have a different configuration and RabbitMQ may still work; the check looks in common places, but may not work for all installations. They are meant to be helpful, but are not required.
You can help by updating the code for other common configurations. Just fork the current repo, add your change, and create a pull request (no other changes please) and I'll pull it back in.
python util_about.py
python util_aboutenv.py
python util_aboutrabbit.py
pip list
- Read the RabbitMQ Hello World! tutorial
- Read the code and comments in our 2 project files: emit_message.py and listen_for_messages.py
Don't worry if it doesn't all make sense the first time. Approach it like a puzzle and see what you can figure out.
- Read v1_emit_message.py (and the tutorial)
- Run the file.
It will run, emit a message to the named RabbitMQ queue, and finish. We can execute additional commands in the terminal as soon as it finishes.
- Read v1_listen_for_messages.py (and the tutorial)
- Run the file.
You'll need to fix an error in the program to get it to run. Once it runs successfully, will it terminate on its own? How do you know? As long as the process is running, we cannot use this terminal for other commands.
- Open a new terminal window.
- Use this new window to run emit_message.py again.
- Watch the listing terminal - what do you see? A second message?
Sending the same message each time is kind of boring. This time:
- Where is the message defined? How can you change it?
- Modify emit_message.py to emit a different message.
- Execute the updated emit_message.py.
- Watch what happens in the listening terminal.
Repeat this process several times - emit at least 4 different messages. Don't worry - it's just code. We can always revert back (try the 'undo' command in VS Code) to a version that works. You can't hurt anything.
Did you notice you had to change the message in TWO places?
- You update the actual message sent.
- You also update what is displayed to the user.
- Fix this by introducing a variable to hold the message.
- Use your variable when sending.
- Use the variable again when displaying to the user.
Now, to send a new message, you'll only make ONE change. Updating and improving code is called 'refactoring'. Use your skills to keep coding enjoyable.
Now look at the second version of each file. These include more graceful error handling, and a consistent, reusable approach to building code.
Each of the version 2 programs include an error as well.
- Find the error and fix it.
- Compare the structure of the version 2 files.
- Modify the docstrings on all your files.
- Include your name and the date.
- Imports always go at the top, just after the file docstring.
- Imports should be one per line - why?
- Then, define your functions.
- Functions are reusable logic blocks.
- Everything the function needs comes in through the arguments.
- A function may - or may not - return a value.
- When we open a connection, we should close the connection.
- Which of the 4 files will always close() the connection?
- Search GitHub for if name == "main":
- How many hits did you get?
- Learn and understand this common Python idiom.