Book Image

Hands-On Enterprise Application Development with Python

By : Saurabh Badhwar
Book Image

Hands-On Enterprise Application Development with Python

By: Saurabh Badhwar

Overview of this book

Dynamically typed languages like Python are continuously improving. With the addition of exciting new features and a wide selection of modern libraries and frameworks, Python has emerged as an ideal language for developing enterprise applications. Hands-On Enterprise Application Development with Python will show you how to build effective applications that are stable, secure, and easily scalable. The book is a detailed guide to building an end-to-end enterprise-grade application in Python. You will learn how to effectively implement Python features and design patterns that will positively impact your application lifecycle. The book also covers advanced concurrency techniques that will help you build a RESTful application with an optimized frontend. Given that security and stability are the foundation for an enterprise application, you’ll be trained on effective testing, performance analysis, and security practices, and understand how to embed them in your codebase during the initial phase. You’ll also be guided in how to move on from a monolithic architecture to one that is service oriented, leveraging microservices and serverless deployment techniques. By the end of the book, you will have become proficient at building efficient enterprise applications in Python.
Table of Contents (24 chapters)
Title Page
Copyright and Credits
About Packt
Contributors
Preface
Index

Chapter 5


Answer 1

For handling requests through the use of multiple application instances, we use the concept of horizontal scaling, where we launch more than one instance of the same application behind a load balancer. The load balancer is then responsible for distributing the incoming requests across this pool of application instances.

Answer 2

The process pools can be implemented through the use of ProcessPoolExecutor from the concurrent.futures library in Python. An example of how to use ProcessPoolExecutor to distribute the requests over a pool can be seen in the Using thread pools for handling incoming connections section of this chapter.

Answer 3

It is completely possible to have a program that combines the use of multiprocessing and multithreading. The following snippet of code shows this implementation:

import threading
import multiprocessing

def say_hello():
    print("Hello")

def start_threads():
    thread_pool = []
    for _ in range(5):
        thread = threading.Thread(target=say_hello)
        thread_pool.append(thread)
    for thread in thread_pool:
        thread.start()
    for thread in thread_pool:
        thread.join()

def start_process():
    process_pool = []
    for _ in range(3):
        process = multiprocessing.Process(target=start_threads)
        process_pool.append(process)
    for process in process_pool:
        process.start()
    for process in process_pool:
        process.join()

if __name__ == '__main__':
    start_process()

The preceding way of achieving this is valid and can be easily implemented without any issues, though you might find that it has a limited number of use cases, and its use will be limited by the implementation of the GIL.

Answer 4

A simple example of implementing a socket server is shown in the Implementing a simple socket server with AsyncIO section of this chapter. Another way is to implement a fully functional web server through the use of AsyncIO is by using the aiohttp framework, which provides an AIO-based HTTP server.