Book Image

Python Microservices Development

Book Image

Python Microservices Development

Overview of this book

We often deploy our web applications into the cloud, and our code needs to interact with many third-party services. An efficient way to build applications to do this is through microservices architecture. But, in practice, it's hard to get this right due to the complexity of all the pieces interacting with each other. This book will teach you how to overcome these issues and craft applications that are built as small standard units, using all the proven best practices and avoiding the usual traps. It's a practical book: you’ll build everything using Python 3 and its amazing tooling ecosystem. You will understand the principles of TDD and apply them. You will use Flask, Tox, and other tools to build your services using best practices. You will learn how to secure connections between services, and how to script Nginx using Lua to build web application firewall features such as rate limiting. You will also familiarize yourself with Docker’s role in microservices, and use Docker containers, CoreOS, and Amazon Web Services to deploy your services. This book will take you on a journey, ending with the creation of a complete Python application based on microservices. By the end of the book, you will be well versed with the fundamentals of building, designing, testing, and deploying your Python microservices.
Table of Contents (20 chapters)
Title Page
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface
Introduction

Iterators and generators


To understand how asynchronous programming works in Python, it is important to first understand how iterators and generators work because they are the basis of asynchronous features in Python.

An iterator in Python is a class that implements the Iterator protocol. The class must implement the following two methods:

  • __iter__(): ; ;Returns the actual iterator. It often returns self
  • next(): ;Returns the next value until StopIteration() is raised

In the following example, we'll implement the Fibonacci sequence as an iterator:

    class Fibo: 
        def __init__(self, max=10): 
            self.a, self.b = 0, 1 
            self.max = max 
            self.count = 0 
 
        def __iter__(self): 
            return self 
 
        def next(self): 
            try: 
                return self.a 
            finally: 
                if self.count == self.max: 
                    raise StopIteration() 
                self.a, self.b = self.b, self.a + self.b 
         ...