0%

Book Description

A comprehensive guide to help you understand the principles of Reactive and asynchronous programming and its benefits

Key Features

  • Explore the advantages of Reactive programming
  • Use concurrency and parallelism in RxPY to build powerful reactive applications
  • Deploy and scale your reactive applications using Docker

Book Description

Reactive programming is central to many concurrent systems, but it's famous for its steep learning curve, which makes most developers feel like they're hitting a wall. With this book, you will get to grips with reactive programming by steadily exploring various concepts

This hands-on guide gets you started with Reactive Programming (RP) in Python. You will learn abouta the principles and benefits of using RP, which can be leveraged to build powerful concurrent applications. As you progress through the chapters, you will be introduced to the paradigm of Functional and Reactive Programming (FaRP), observables and observers, and concurrency and parallelism. The book will then take you through the implementation of an audio transcoding server and introduce you to a library that helps in the writing of FaRP code. You will understand how to use third-party services and dynamically reconfigure an application.

By the end of the book, you will also have learned how to deploy and scale your applications with Docker and Traefik and explore the significant potential behind the reactive streams concept, and you'll have got to grips with a comprehensive set of best practices.

What you will learn

  • Structure Python code for better readability, testing, and performance
  • Explore the world of event-based programming
  • Grasp the use of the most common operators in Rx
  • Understand reactive extensions beyond simple examples
  • Master the art of writing reusable components
  • Deploy an application on a cloud platform with Docker and Traefik

Who this book is for

If you are a Python developer who wants to learn Reactive programming to build powerful concurrent and asynchronous applications, this book is for you. Basic understanding of the Python language is all you need to understand the concepts covered in this book.

Downloading the example code for this book You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. Hands-On Reactive Programming with Python
  3. Dedication
  4. Packt Upsell
    1. Why subscribe?
    2. Packt.com
  5. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Conventions used
    4. Get in touch
      1. Reviews
  7. An Introduction to Reactive Programming
    1. What is reactive programming?
      1. Event-driven programming
      2. Reactive versus proactive
      3. Reactor and proactor
      4. Reactive systems
    2. Introduction to ReactiveX and RxPY
      1. ReactiveX principles
      2. Operators
      3. Installating RxPY
    3. A reactive echo application
    4. Marble diagrams
      1. The map operator
      2. The from_ operator
    5. Flow diagrams
      1. Reactivity diagrams
      2. Reactivity diagram elements
      3. Reactivity diagrams of an echo example
    6. Summary
    7. Questions
    8. Further reading
  8. Asynchronous Programming in Python
    1. What is asynchronous programming?
    2. The history of asynchronous programming in Python
      1. Asynchronous handlers with callbacks
      2. Asynchronous handlers with generators
        1. Understanding generators
        2. Using generators for asynchronous handlers
    3. Introduction to AsyncIO
      1. Futures
      2. Coroutines
      3. Event loop
    4. An HTTP echo server
      1. aiohttp
      2. An AsyncIO HTTP server
    5. Summary
    6. Questions
    7. Further reading
  9. Functional Programming with ReactiveX
    1. What is functional programming?
      1. Some base elements of functional programming
        1. Lambdas
        2. Closures
        3. Side effects and pure functions
        4. Higher-order functions
      2. Functional reactive programming
    2. Observable cycles
      1. The observable cycle issue
      2. ReactiveX Subject
      3. Solving the cycle issue with Subject
    3. Structuring functional and reactive code
    4. The HTTP echo server with an HTTP driver
      1. ReactiveX operators
        1. The empty operator
        2. The merge operator
      2. Implementation of the HTTP server driver
      3. Bootstrapping the event loop
      4. Adding the program logic
    5. Summary
    6. Questions
    7. Further reading
  10. Exploring Observables and Observers
    1. Creating observables
      1. Creating observables from values
        1. The of operator
        2. The just operator
        3. The range operator
        4. The repeat operator
        5. The never and throw operators
      2. Observables driven by time
        1. The timer operator
        2. The interval operator
      3. Custom observables
        1. The from_callback operator
        2. The create operator
    2. Hot and cold observables
      1. Operators – publish and connect
      2. Operators – ref_count and share
    3. Subscription and disposal
      1. Subscribing to an observable
      2. Disposing a subscription
      3. Custom disposal
    4. Error handling
      1. The catch_exception operator
      2. The retry operator
    5. Observables and AsyncIO
      1. The start operator
      2. The from_future operator
    6. Summary
    7. Questions
  11. Concurrency and Parallelism in RxPY
    1. Concurrency and schedulers
    2. Available schedulers
      1. The NewThread scheduler
      2. The ThreadPool scheduler
      3. The AsyncIO scheduler
    3. Summary
    4. Questions
  12. Implementation of an Audio Transcoding Server
    1. Technical requirements
    2. Structuring the project
      1. The base structure
      2. Maximizing code readability
        1. Using named tuples
        2. Using dataclasses
        3. Avoiding backslashes
    3. Introduction to Cyclotron
      1. The command-line echo example
    4. Operators used in this application
      1. The skip operator
      2. The filter operator
      3. The flat_map operator
      4. The let operator
    5. Implementation of the encoding server
      1. Parsing the configuration file
      2. Implementation of the encoder driver
        1. Using the encoder driver
      3. Exposing the REST APIs
      4. Putting it all together
      5. Using the encoding server
    6. Summary
    7. Questions
    8. Further reading
  13. Using Third-Party Services
    1. Technical requirements
    2. An introduction to S3 storage
    3. Installing Docker and Minio
      1. An introduction to Docker
        1. Docker installation on Linux
        2. Getting started with Docker
      2. Using Minio as S3 storage
    4. Using S3 as storage
      1. Implementation of the S3 driver
      2. Returning data in the encoding driver
      3. Using the S3 driver
    5. Dealing with blocking API and CPU-bound operations
      1. Using thread pools for CPU-bound operations
      2. Moving the blocking I/O to a dedicated thread
    6. Summary
    7. Questions
    8. Further reading
  14. Dynamic Reconfiguration and Error Management
    1. Monitoring file changes
      1. Understanding inotify
      2. Monitoring file changes with inotify
    2. Operators used for dynamic configuration
      1. The debounce operator
      2. The take operator
      3. The distinct_until_changed operator
      4. The start_with operator
      5. The combine_latest operator
    3. Encode driver reconfiguration
      1. Configuring the audio encoding
      2. Monitoring the configuration file
        1. Implementing an inotify driver
        2. Monitoring changes in the configuration file
    4. Error management
    5. Summary
    6. Questions
  15. Operators in RxPY
    1. Transforming observables
      1. The buffer operator
      2. The window operator
      3. The group_by operator
    2. Filtering observables
      1. The first operator
      2. The last operator
      3. The skip_last operator
      4. The take_last operator
      5. The ignore_elements operator
      6. The sample operator
      7. The distinct operator
      8. The element_at operator
    3. Combining observables
      1. The join operator
      2. The switch_latest operator
      3. The zip operator
      4. The zip_list operator
    4. Utility operators
      1. The delay operator
      2. The do_action operator
      3. The materialize/dematerialize operators
      4. The time_interval operator
      5. The timeout operator
      6. The timestamp operator
      7. The using operator
      8. The to_list operator
    5. Conditional operators
      1. The all operator
      2. The amb operator
      3. The contains operator
      4. The default_if_empty operator
      5. The sequence_equal operator
      6. The skip_until operator
      7. The skip_while operator
      8. The take_until operator
      9. The take_while operator
    6. Mathematical operators
      1. The average operator
      2. The concat operator
      3. The count operator
      4. The max operator
      5. The min operator
      6. The reduce operator
      7. The sum operator
    7. Summary
    8. Questions
    9. Further reading
  16. Testing and Debugging
    1. Testing
      1. Introduction to Python unit testing
        1. Dependency injection versus mock
      2. Testing a custom operator
      3. Injecting asynchronous obervables
      4. Testing AsyncIO code
    2. Logging
    3. Debugging
      1. Adding traces
      2. Debugging AsyncIO
    4. Summary
    5. Questions
    6. Further reading
  17. Deploying and Scaling Your Application
    1. Technical requirements
    2. Introduction to Traefik
      1. Reverse proxies and load balancers
        1. What is a reverse proxy?
        2. What is a load balancer?
      2. Traefik principles
    3. Packaging a service with Docker Compose
      1. Introduction to Docker Compose
        1. Installing Docker Compose
      2. Starting Traefik
      3. Packaging the audio transcoder
        1. How images are composed together
      4. Composing all services
      5. Using containers for horizontal scaling
    4. Adding TLS support with Let's Encrypt
      1. Introduction to Let's Encrypt
      2. Deploying on a public server
      3. Enabling the ACME protocol
    5. Summary
    6. Questions
    7. Further reading
  18. Reactive Streams for Remote Communication
    1. Communication patterns and observables
      1. Publish/subscribe
      2. Channel
      3. Request/response
    2. Observable multiplexing
      1. Framing
        1. An implementation of line-based framing
      2. Serialization
        1. An implementation of JSON serialization
      3. Routing
    3. Implementing publish/subscribe
      1. The publisher
        1. Implementation of a TCP server
        2. Implementation of the publisher
      2. The subscriber
        1. Implementation of a TCP client
        2. Implementation of the subscriber
    4. Summary
    5. Questions
    6. Further reading
  19. A Checklist of Best Practices
    1. The observable creation decision tree
    2. Keeping side-effects as small as possible
    3. What to do when nothing happens
    4. Miscellaneous recommendations
      1. Favor composition of existing operators
      2. Always specify a scheduler
      3. Subscriptions should not throw exceptions
      4. Disposal should not throw exceptions
      5. Be clear on hot and cold observables
    5. Summary
    6. Questions
    7. Further reading
  20. Assessments
    1. Chapter 1
    2. Chapter 2
    3. Chapter 3
    4. Chapter 4
    5. Chapter 5
    6. Chapter 6
    7. Chapter 7
    8. Chapter 8
    9. Chapter 9
    10. Chapter 10
    11. Chapter 11
    12. Chapter 12
    13. Chapter 13
  21. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think
3.144.30.62