Code Monkey home page Code Monkey logo

streaming-03-rabbitmq's Introduction

streaming-03-rabbitmq

image

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.

Prerequisites

  1. Git
  2. Python 3.7+ (3.11+ preferred)
  3. VS Code Editor
  4. VS Code Extension: Python (by Microsoft)
  5. RabbitMQ Server installed and running locally

Before You Begin

  1. Fork this starter repo into your GitHub account.
  2. Clone your repo down to your machine.
  3. Explore your new project repo in VS Code on your local machine.

Task 1. Create a Python Virtual Environment

We will create a local Python virtual environment to isolate our project's third-party dependencies from other projects.

  1. Open a terminal window in VS Code.
  2. 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.

Task 2. Activate the Virtual Environment

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.

Task 3. Install Dependencies into the Virtual Environment

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

Task 4. Verify Setup (OPTIONAL - ONLY WORK ON SOME CONFIGURATIONS)

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

verifying setup

Task 5. Read

  1. Read the RabbitMQ Hello World! tutorial
  2. 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.

Task 6. Execute the Producer/Sender

  1. Read v1_emit_message.py (and the tutorial)
  2. 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.

Task 7. Execute the Consumer/Listener

  1. Read v1_listen_for_messages.py (and the tutorial)
  2. 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.

Task 8. Open a New Terminal / Emit More Messages

  1. Open a new terminal window.
  2. Use this new window to run emit_message.py again.
  3. Watch the listing terminal - what do you see? A second message?

Sending the same message each time is kind of boring. This time:

  1. Where is the message defined? How can you change it?
  2. Modify emit_message.py to emit a different message.
  3. Execute the updated emit_message.py.
  4. 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.

Task 9. Save Time & Effort: Don't Repeat Yourself

Did you notice you had to change the message in TWO places?

  1. You update the actual message sent.
  2. You also update what is displayed to the user.
  3. Fix this by introducing a variable to hold the message.
  4. Use your variable when sending.
  5. 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.

Version 2

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.

  1. Find the error and fix it.
  2. Compare the structure of the version 2 files.
  3. Modify the docstrings on all your files.
  4. Include your name and the date.
  5. Imports always go at the top, just after the file docstring.
  6. Imports should be one per line - why?
  7. Then, define your functions.
  8. Functions are reusable logic blocks.
  9. Everything the function needs comes in through the arguments.
  10. A function may - or may not - return a value.
  11. When we open a connection, we should close the connection.
  12. Which of the 4 files will always close() the connection?
  13. Search GitHub for if name == "main":
  14. How many hits did you get?
  15. Learn and understand this common Python idiom.

Reference

Exploring the local virtual environment folder

streaming-03-rabbitmq's People

Contributors

denisecase avatar stone-snevets avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.