Coder Social home page Coder Social logo

geckog / nw-streaming03-rabbitmq Goto Github PK

View Code? Open in Web Editor NEW

This project forked from denisecase/streaming-03-rabbitmq

0.0 0.0 0.0 2.08 MB

Get started with RabbitMQ, a message broker, that enables multiple processes to communicate reliably through an intermediary

Python 100.00%

nw-streaming03-rabbitmq's Introduction

streaming-03-rabbitmq

Get started with RabbitMQ, a message broker, that enables multiple processes to communicate reliably through an intermediary

Before You Begin

  1. Fork this starter repo into your GitHub.
  2. Clone your repo down to your machine.
  3. In VS Code with Python extension, click on emit_message_v1.py to get VS Code in Python mode.
  4. View / Command Palette - then Python: Select Interpreter
  5. Select your conda environment. See the references below for more.
  6. Use the terminal to install pika into your active environment.

conda install -c conda-forge pika

Read

  1. Read the RabbitMQ Tutorial - Hello, World!
  2. Read the code and comments in this repo.

Execute about,py

  1. Run about.py.
  2. Read about.txt.
  3. Verfiy you have exactly one active, one None env.

Version 1 - Execute the Producer/Sender

  1. Read v1_emit_message.py (and the tutorial)
  2. Run the file.

You'll need to fix an error in the program to get it to run.

+ Looks like the error was due to an extra letter in 'localhost'

Once it runs and finishes, we can reuse the terminal.

Version 1 - 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?

+ The process just waits to receive messages, so it will not terminate unless we force it to.

As long as the process is running, we cannot use this terminal for other commands.

Version 1 - Open a New Terminal / Emit More Messages

  1. Open a new terminal window.
  2. Use this new window to emit more messages
  3. In v1_emit_message.py, modify the message.
  4. Execute the script.
  5. Watch what happens in the listening window.
  6. Do this several times to emit at least 3 different messages.

Version 1: Don't Repeat Yourself (DRY)

  1. 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.
  2. Fix this by introducting a variable to hold the message.
    1. Use your variable when sending.
    2. Use the variable again when displaying to the user.
+ Used a variable called 'messagetext'

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.
+ localhostttt was used instead of localhost
  1. Compare the structure of the version 2 files.
+ Version 2 files both use the 'if name == "main"' idiom.
+ They also both define reusable functions with arguments.
+ The receiver, which is more prone to errors, has a couple exception handlers.
  1. Modify the docstrings on all your files.
  2. Include your name and the date.
  3. Imports always go at the top, just after the file docstring.
  4. Imports should be one per line - why?
+ When errors arise, it tells you what line it is in. If all the imports
+ on the same line, it will be harder to know which one caused the error.
  1. Then, define your functions.
  2. Functions are reuable logic blocks.
  3. Everything the function needs comes in through the arguments.
  4. A function may - or may not - return a value.
  5. When we open a connection, we should close the connection.
  6. Which of the 4 files will always close() the connection?
+ v1_emit_message is the only one that will always close() connection.
+ The receivers both go indefinitely, while v2_emit_message only opens and
+ closes connection when the send_message function is called.
  1. Search GitHub for if name == "main":
  2. How many hits did you get?
+ 26Million code results and 1900 discussions
  1. Learn and understand this common Python idiom.
+ If I wanted to import this into another python script, I could call its
+ functions without executing the lines of code under that if statement.
+ Essentially, it allows the script to be executed both on its own and as a module

Reference

Multiple Terminals

img

nw-streaming03-rabbitmq's People

Contributors

denisecase avatar geckog 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.