Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Hands-On Reactive Programming with Python
Hands-On Reactive Programming with Python

Hands-On Reactive Programming with Python: Event-driven development unraveled with RxPY

eBook
€8.99 €29.99
Paperback
€36.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Hands-On Reactive Programming with Python

Asynchronous Programming in Python

his chapter covers the principles of asynchronous programming and the AsyncIO library. It is composed of three parts. The first one explains what asynchronous programming is, how it is different from synchronous programming, and why and when it is more efficient than synchronous programming. This first part also goes through the history of asynchronous programming in Python to allow you to better understand the current state of Python in this domain. The second part is dedicated to the AsyncIO library. AsyncIO, the official Python asynchronous library, contains all features needed to implement readable asynchronous code. The last part explains how to implement a real application and, more specifically, an HTTP server. By the end of this chapter, you will be able to write an asynchronous network service with AsyncIO.

The following topics will...

What is asynchronous programming?

Chapter 1, An Introduction to Reactive Programming, discussed in detail the principles of event-driven programming and reactive programming. As explained in the previous chapter, event-driven programming can be implemented in many ways: with different programming paradigms such as imperative or object-oriented, but also with different concurrency mechanisms. Asynchronous programming is one way to manage concurrency.

Concurrency is several tasks competing for the same resource at the same time. The definition of "at the same time" can be different depending on the resource being used. There are two main resources that a task may require: a Central Processing Unit (CPU) and I/O. In most cases, a task is either CPU bound or I/O bound; that is, either a task makes a lot of computations and is constrained by the available CPU resources,...

The history of asynchronous programming in Python

Since the early days of Python, it has always been possible to do asynchronous programming, but in the old way; that is, by using callbacks. Chapter 1, An Introduction to Reactive Programming, already explained some of the evolution in the frameworks and programming languages that made asynchronous programming easier during the last few years. The Python language naturally followed that trend, and many incremental improvements have been made since the early 2000s. Figure 2.4 shows the history of the main changes that occurred in Python concerning asynchronous programming.

The evolution on the left side are the elements that are still part of Python. The evolution on the right side concerns asynchronous frameworks and one deprecated module of the standard library:

Figure 2.4: The main steps of asynchronous programming in Python...

Introduction to AsyncIO

AsyncIO is the official asynchronous framework for Python. As explained in the previous section, this does not mean that it is the only one available. Twisted and Gevent are two other asynchronous frameworks that are very famous. However, since its release in Python 3.4, AsyncIO has become very popular rather quickly. There is no doubt that one reason for this success comes from the philosophy of Python: There should be only one obvious way to do something. Since AsyncIO is part of the standard library, it is the de facto way to do asynchronous programming in Python. Another reason is probably that it was released with the correct timing, when developers regained interest in asynchronous programming.

AsyncIO relies on three entities:

  • Futures: They represent values that will be available later
  • Coroutines: They allow you to execute asynchronous actions
  • ...

An HTTP echo server

This section details a simple but realistic implementation of an asynchronous network service: an echo HTTP server. The behavior is the same as in the reactive echo server implemented in Chapter 1, An Introduction to Reactive Programming, but instead of echoing the command-line arguments, the server sends back the requests it receives on an HTTP request.

aiohttp

The AsyncIO library does not contain an HTTP stack. In this example, as well as throughout this whole book, the aiohttp package will be used. The aiohttp package is a complete implementation of the HTTP 1.x protocol with the support of web sockets. It can be used to implement clients and servers. This is a great package to implement HTTP microservices...

Summary

This chapter covered all the base concepts of asynchronous programming and how they work in Python. You should now understand what it means to yield or await a coroutine and know the differences between blocking and non-blocking designs. Hopefully, the short history of asynchronous programming in Python and the examples of asynchronous programming with callback also helped you understand why Python is one of the few languages with best-in-class support of asynchronous programming.

Moreover, you should also be familiar enough with the AsyncIO APIs to write your own asynchronous applications. This chapter only covered the basic usage of AsyncIO, but the features presented here are the most-used ones. Other more advanced features will be introduced in this book, as they are needed in other examples.

One last word on this chapter: Do not be fooled by the simplicity of the...

Questions

  • Why is asynchronous programming more efficient than multiple processes/threads to handle I/O concurrency?
  • Why is asynchronous programming not a solution to improve the performance of CPU-bound tasks?
  • What is the reason why multithreading is not performing as well in Python as in other programming languages?
  • What is the benefit of a generator compared to an iterator?
  • Why does a generator help in writing asynchronous code?
  • What is the difference between calling the next function and calling the send method of a generator object?
  • How is a coroutine declared?
  • How can a coroutine call another coroutine?
  • What is the role of the event loop?

Further reading

The evolution of the Python language is documented in Python Enhancement Proposals (PEPs). The three main evolution points of asynchronous programming in Python are described in detail in these dedicated PEPs:

The documentation of the AsyncIO package is in the Python official documentation. Do not hesitate to read it to see all features available with event loops, futures, and coroutines: https://docs.python.org/3/library/asyncio.html.

The documentation of the aiohttp package is very detailed, and contains examples. This package will be used throughout this whole book to write microservices...

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • 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

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.

Who is this book 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.

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

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Oct 25, 2018
Length: 420 pages
Edition : 1st
Language : English
ISBN-13 : 9781789132755
Category :
Languages :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Oct 25, 2018
Length: 420 pages
Edition : 1st
Language : English
ISBN-13 : 9781789132755
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 108.97
Hands-On Reactive Programming with Python
€36.99
Python 3 Object-Oriented Programming
€34.99
Mastering Concurrency in Python
€36.99
Total 108.97 Stars icon
Banner background image

Table of Contents

15 Chapters
An Introduction to Reactive Programming Chevron down icon Chevron up icon
Asynchronous Programming in Python Chevron down icon Chevron up icon
Functional Programming with ReactiveX Chevron down icon Chevron up icon
Exploring Observables and Observers Chevron down icon Chevron up icon
Concurrency and Parallelism in RxPY Chevron down icon Chevron up icon
Implementation of an Audio Transcoding Server Chevron down icon Chevron up icon
Using Third-Party Services Chevron down icon Chevron up icon
Dynamic Reconfiguration and Error Management Chevron down icon Chevron up icon
Operators in RxPY Chevron down icon Chevron up icon
Testing and Debugging Chevron down icon Chevron up icon
Deploying and Scaling Your Application Chevron down icon Chevron up icon
Reactive Streams for Remote Communication Chevron down icon Chevron up icon
A Checklist of Best Practices Chevron down icon Chevron up icon
Assessments Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.