Rxpy 简明教程

RxPY - Overview

本章解释了响应式编程、RxPY 以及它的运算符、特性、优点和缺点。

This chapter explains what is reactive programming, what is RxPY, its operators, features, advantages and disadvantage.

What is Reactive Programming?

响应式编程是一种编程范例,用于处理数据流和变更传播。这意味着,当一个数据流由一个组件发出时,变更将会由一个响应式编程的库传播到其他组件。变更会持续传播,直到它到达最终接收器。

Reactive programming is a programming paradigm, that deals with data flow and the propagation of change. It means that, when a data flow is emitted by one component, the change will be propagated to other components by a reactive programming library. The propagation of change will continue until it reaches the final receiver.

通过使用 RxPY,你可以很好地控制异步数据流,例如,可以利用可观察对象追踪对 URL 发出的请求,并使用观察器侦听请求完成以取得响应或错误信息。

By using RxPY, you have good control on the asynchronous data streams, for example, a request made to URL can be traced by using observable, and use the observer to listen to when the request is complete for response or error.

RxPY 可让你使用 Observables 处理异步数据流,使用 Operators 查询数据流,即筛选、求和、连接、映射,还可利用 Schedulers 对数据流实现并发。创建一个可观察对象会提供一个观察器对象,其中有 on_next(v)、on_error(e) 和 on_completed() 方法,这些方法需要 subscribed ,以便我们在事件发生时获得通知。

RxPY offers you to handle asynchronous data streams using Observables, query the data streams using Operators i.e. filter, sum, concat, map and also make use of concurrency for the data streams using Schedulers. Creating an Observable, gives an observer object with on_next(v), on_error(e) and on_completed() methods, that needs to be subscribed so that we get a notification when an event occurs.

observable

可观察对象可以使用管道运算符按链式格式利用多个运算符进行查询。

The Observable can be queried using multiple operators in a chain format by using the pipe operator.

RxPY 在不同类别中提供运算符,例如: −

RxPY offers operators in various categories like:−

  1. Mathematical operators

  2. Transformation operators

  3. Filtering operators

  4. Error handling operators

  5. Utility operators

  6. Conditional operators

  7. Creation operators

  8. Connectable operators

本教程将详细解释这些运算符。

These operators are explained in detail in this tutorial.

What is RxPy?

RxPy 定义为 使用Python 中的可观察对象集合和可管道查询运算符组合异步和事件驱动程序的库,这是 RxPy 官方网站中的定义,网址为 https://rxpy.readthedocs.io/en/latest/.

RxPY is defined as *a library for composing asynchronous and event-based programs using observable collections and pipable query operators in Python *as per the official website of RxPy, which is https://rxpy.readthedocs.io/en/latest/.

RxPY 是一个 Python 库,用于支持响应式编程。RxPy 代表 Reactive Extensions for Python 。它是一个库,使用可观察对象来处理响应式编程,用于处理异步数据调用、回调和事件驱动程序。

RxPY is a python library to support Reactive Programming. RxPy stands for Reactive Extensions for Python. It’s a library that uses observables to work with reactive programming that deals with asynchronous data calls, callbacks and event−based programs.

Features of RxPy

在 RxPy 中,以下概念用于处理异步任务 −

In RxPy, following concepts take care of handling the asynchronous task −

Observable

可观察对象是一个创建观察器并将其附加到预期的具有数据流的源的函数,例如推文、与计算机相关事件等。

An observable is a function that creates an observer and attaches it to the source having data streams that are expected from, for example, Tweets, computer−related events, etc.

Observer

它是一个对象,具有 on_next()、on_error() 和 on_completed() 方法,当可观察对象与可观察对象发生交互时,这些方法将被调用,即源与可观察对象进行交互,例如输入的推文等。

It is an object with on_next(), on_error() and on_completed() methods, that will get called when there is interaction with the observable i.e. the source interacts for an example incoming Tweets, etc.

Subscription

当创建可观察对象时,我们需要订阅它以执行可观察对象。

When the observable is created, to execute the observable we need to subscribe to it.

Operators

运算符是一个纯函数,它获取可观察对象作为输入,并且输出也是一个可观察对象。你可以使用管道运算符对可观察数据对象使用多个运算符。

An operator is a pure function that takes in observable as input and the output is also an observable. You can use multiple operators on an observable data by using the pipe operator.

Subject

一个主题是一个可观察序列,同时也是一个观察器,它可以多播,即可以与订阅它的多个观察器进行通信。主题是一个冷可观察对象,这意味着值将在已订阅的观察器之间共享。

A subject is an observable sequence as well as an observer that can multicast, i.e. talk to many observers that have subscribed. The subject is a cold observable, i.e. the values will be shared between the observers that have been subscribed.

Schedulers

RxPy 的一个重要特性是并发,即允许任务并行执行。为了实现这一点,RxPy 有两个运算符 subscribe_on() 和 observe_on(),它们与调度程序一起工作,并将决定已订阅任务的执行。

One important feature of RxPy is concurrency i.e. to allow the task to execute in parallel. To make that happen RxPy has two operators subscribe_on() and observe_on() that works with schedulers and will decide the execution of the subscribed task.

Advantages of using RxPY

以下是 RxPy 的优点 −

The following are the advantages of RxPy −

  1. RxPY is an awesome library when it comes to the handling of async data streams and events. RxPY uses observables to work with reactive programming that deals with asynchronous data calls, callbacks and event-based programs.

  2. RxPY offers a huge collection of operators in mathematical, transformation, filtering, utility, conditional, error handling, join categories that makes life easy when used with reactive programming.

  3. Concurrency i.e. working of multiple tasks together is achieved using schedulers in RxPY.

  4. The performance is improved using RxPY as handling of async task and parallel processing is made easy.

Disadvantage of using RxPY

  1. Debugging the code with observables is a little difficult.