Hands-On Reactive Programming with Python
eBook - ePub

Hands-On Reactive Programming with Python

Event-driven development unraveled with RxPY

  1. 420 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Hands-On Reactive Programming with Python

Event-driven development unraveled with RxPY

About this book

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.

Tools to learn more effectively

Saving Books

Saving Books

Keyword Search

Keyword Search

Annotating Text

Annotating Text

Listen to it instead

Listen to it instead

Information

Operators in RxPY

The previous chapters covered more than 30 operators. These operators allow us to implement a realistic application, but many others are often needed. This chapter describes more than 40 other operators that are also commonly used, grouped by functional category. As usual, each operator is described with its marble diagram, a comprehensive description, and an example of how to use it to help you decide when to use what operator.
The following topics will be covered in this chapter:
  • Transforming observables
  • Filtering observables
  • Combining observables
  • Utility operators
  • Conditional operators
  • Mathematical operators

Transforming observables

The following operators allow us to make some transformations on the items emitted on source observables. Some of them have already been covered in the previous chapters. They are the following:
Operator Description Chapter
map Maps each item to another value 1
flat_map Maps each item to an observable and flattens all emitted items in a single observable 6

The buffer operator

The buffer operator groups items emitted on the source observable based on different types of window selectors. These selectors can be defined via another observable, an item count, or time information. The following figure shows the marble diagram of the buffer operator:
Figure 9.1: The buffer operator
This operator has several prototypes:
Observable.buffer(self, buffer_openings=None, 
buffer_closing_selector=None)
Observable.buffer_with_count(self, count, skip=None)
Observable.buffer_with_time(self, timespan,
timeshift=None, scheduler=None)
Observable.buffer_with_time_or_count(self,
timespan, count, scheduler=None)
The first version allows us to use another observable to control when a new buffer begins. Each time the buffer_openings observable emits an item, then the current buffer is emitted, and a new buffer is created. The buffer_closing_selector parameter is an optional parameter. When provided, it must be a function that returns an observable whose completion ends the current buffer. On completion, this function will be called again to start a new buffer. The buffer_closing_selector parameter can also be provided as the first parameter of this operator. If the first parameter is an observable, then it is used as the buffer_openings parameters, and if it is a function, then it is used as the buffer_closing_selector parameter.
The following is the first way to use the buffer operator:
numbers = Subject()
windows = Subject()
numbers.buffer(windows).subscribe(
on_next = lambda i: print("on_next {}".format(i)),
on_error = lambda e: print("on_error: {}".format(e)),
on_completed = lambda: print("on_completed")
)

numbers.on_next(1)
numbers.on_next(2)
windows.on_next(True)
numbers.on_next(3)
numbers.on_next(4)
numbers.on_next(5)
windows.on_next(True)
The numbers observable is the observable that must be buffered. The windows observable emits items each time a new buffering window must start. The following example gives the following result:
on_next [1, 2]
on_next [3, 4, 5]
The first two items are emitted as a single list item. Then, the next three items are emitted as a second item.
The closing_selector parameter can be used in the following way:
window_selector = None
def closing_selector():
print("closing_selector")
global window_selector
window_selector = Subject()
return window_selector

numbers = Subject()
numbers.buffer(closing_selector).subscribe(
on_next = lambda i: print("on_next {}".format(i)),
on_error = lambda e: print("on_error: {}".format(e)),
on_completed = lambda: print("on_completed")
)

numbers.on_next(1)
numbers.on_next(2)
numbers.on_next(3)
window_selector.on_completed()
numbers.on_next(4)
numbers.on_next(5)
window_selector.on_completed()
The numbers observable is the observable that must be buffered. The closing_selector function returns an observable that emits no items. This observable is completed after emitting three items, and then two items. This example gives the following result:
closing_selector on_next [1, 2, 3] closing_selector on_next [4, 5] closing_selector
First, the closing_selector function is called. This allows the buffer operator to subscribe to its completion. When the window_selector observable completes, then the first item is emitted (the 1, 2, 3 list). After that, ...

Table of contents

  1. Title Page
  2. Copyright and Credits
  3. Dedication
  4. Packt Upsell
  5. Contributors
  6. Preface
  7. An Introduction to Reactive Programming
  8. Asynchronous Programming in Python
  9. Functional Programming with ReactiveX
  10. Exploring Observables and Observers
  11. Concurrency and Parallelism in RxPY
  12. Implementation of an Audio Transcoding Server
  13. Using Third-Party Services
  14. Dynamic Reconfiguration and Error Management
  15. Operators in RxPY
  16. Testing and Debugging
  17. Deploying and Scaling Your Application
  18. Reactive Streams for Remote Communication
  19. A Checklist of Best Practices
  20. Assessments
  21. Other Books You May Enjoy

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn how to download books offline
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 990+ topics, we’ve got you covered! Learn about our mission
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more about Read Aloud
Yes! You can use the Perlego app on both iOS and Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app
Yes, you can access Hands-On Reactive Programming with Python by Romain Picard in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming. We have over one million books available in our catalogue for you to explore.